1/*-
2 * Copyright (c) 2011 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27/*
28 * USERBOOT interface versions
29 */
30#define	USERBOOT_VERSION_1      1
31#define	USERBOOT_VERSION_2      2
32#define	USERBOOT_VERSION_3      3
33
34/*
35 * Version 4 added more generic callbacks for setting up
36 * registers and descriptors. The callback structure is
37 * backward compatible (new callbacks have been added at
38 * the tail end).
39 */
40#define	USERBOOT_VERSION_4      4
41
42/*
43 * Version 5 added a callback for indicating that the guest
44 * should be restarted with a different interpreter.  The callback
45 * structure is still backward compatible.
46 */
47#define	USERBOOT_VERSION_5      5
48
49/*
50 * Exit codes from the loader
51 */
52#define	USERBOOT_EXIT_QUIT      1
53#define	USERBOOT_EXIT_REBOOT    2
54
55struct loader_callbacks {
56	/*
57	 * Console i/o
58	 */
59
60        /*
61         * Wait until a key is pressed on the console and then return it
62         */
63	int		(*getc)(void *arg);
64
65        /*
66         * Write the character ch to the console
67         */
68	void		(*putc)(void *arg, int ch);
69
70        /*
71         * Return non-zero if a key can be read from the console
72         */
73	int		(*poll)(void *arg);
74
75	/*
76	 * Host filesystem i/o
77	 */
78
79        /*
80         * Open a file in the host filesystem
81         */
82	int		(*open)(void *arg, const char *filename, void **h_return);
83
84        /*
85         * Close a file
86         */
87	int		(*close)(void *arg, void *h);
88
89        /*
90         * Return non-zero if the file is a directory
91         */
92	int		(*isdir)(void *arg, void *h);
93
94        /*
95         * Read size bytes from a file. The number of bytes remaining
96         * in dst after reading is returned in *resid_return
97         */
98	int		(*read)(void *arg, void *h, void *dst, size_t size,
99            size_t *resid_return);
100
101        /*
102         * Read an entry from a directory. The entry's inode number is
103         * returned in *fileno_return, its type in *type_return and
104         * the name length in *namelen_return. The name itself is
105         * copied to the buffer name which must be at least PATH_MAX
106         * in size.
107         */
108	int		(*readdir)(void *arg, void *h, uint32_t *fileno_return,
109            uint8_t *type_return, size_t *namelen_return, char *name);
110
111        /*
112         * Seek to a location within an open file
113         */
114	int		(*seek)(void *arg, void *h, uint64_t offset,
115            int whence);
116
117        /*
118         * Return some stat(2) related information about the file
119         */
120	int		(*stat)(void *arg, void *h, struct stat *stp);
121
122	/*
123	 * Disk image i/o
124	 */
125
126        /*
127         * Read from a disk image at the given offset
128         */
129	int		(*diskread)(void *arg, int unit, uint64_t offset,
130            void *dst, size_t size, size_t *resid_return);
131
132        /*
133         * Write to a disk image at the given offset
134         */
135	int		(*diskwrite)(void *arg, int unit, uint64_t offset,
136            void *src, size_t size, size_t *resid_return);
137
138	/*
139	 * Guest virtual machine i/o
140	 */
141
142        /*
143         * Copy to the guest address space
144         */
145	int		(*copyin)(void *arg, const void *from,
146            uint64_t to, size_t size);
147
148        /*
149         * Copy from the guest address space
150         */
151	int		(*copyout)(void *arg, uint64_t from,
152            void *to, size_t size);
153
154        /*
155         * Set a guest register value
156         */
157	void		(*setreg)(void *arg, int, uint64_t);
158
159        /*
160         * Set a guest MSR value
161         */
162	void		(*setmsr)(void *arg, int, uint64_t);
163
164        /*
165         * Set a guest CR value
166         */
167	void		(*setcr)(void *arg, int, uint64_t);
168
169        /*
170         * Set the guest GDT address
171         */
172        void            (*setgdt)(void *arg, uint64_t, size_t);
173
174        /*
175         * Transfer control to the guest at the given address
176         */
177	void		(*exec)(void *arg, uint64_t pc);
178
179	/*
180	 * Misc
181	 */
182
183        /*
184         * Sleep for usec microseconds
185         */
186	void		(*delay)(void *arg, int usec);
187
188        /*
189         * Exit with the given exit code
190         */
191	void		(*exit)(void *arg, int v);
192
193        /*
194         * Return guest physical memory map details
195         */
196	void		(*getmem)(void *arg, uint64_t *lowmem,
197            uint64_t *highmem);
198
199	/*
200	 * ioctl interface to the disk device
201	 */
202	int		(*diskioctl)(void *arg, int unit, u_long cmd,
203	    void *data);
204
205	/*
206	 * Returns an environment variable in the form "name=value".
207	 *
208	 * If there are no more variables that need to be set in the
209	 * loader environment then return NULL.
210	 *
211	 * 'num' is used as a handle for the callback to identify which
212	 * environment variable to return next. It will begin at 0 and
213	 * each invocation will add 1 to the previous value of 'num'.
214	 */
215	char *		(*getenv)(void *arg, int num);
216
217	/*
218	 * Version 4 additions.
219	 */
220	int	(*vm_set_register)(void *arg, int vcpu, int reg, uint64_t val);
221	int	(*vm_set_desc)(void *arg, int vcpu, int reg, uint64_t base,
222	    u_int limit, u_int access);
223
224	/*
225	 * Version 5 addition.
226	 */
227	void	(*swap_interpreter)(void *arg, const char *interp);
228};
229