1223695Sdfr/*-
2223695Sdfr * Copyright (c) 2011 Doug Rabson
3223695Sdfr * All rights reserved.
4223695Sdfr *
5223695Sdfr * Redistribution and use in source and binary forms, with or without
6223695Sdfr * modification, are permitted provided that the following conditions
7223695Sdfr * are met:
8223695Sdfr * 1. Redistributions of source code must retain the above copyright
9223695Sdfr *    notice, this list of conditions and the following disclaimer.
10223695Sdfr * 2. Redistributions in binary form must reproduce the above copyright
11223695Sdfr *    notice, this list of conditions and the following disclaimer in the
12223695Sdfr *    documentation and/or other materials provided with the distribution.
13223695Sdfr *
14223695Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15223695Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16223695Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17223695Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18223695Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19223695Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20223695Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21223695Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22223695Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23223695Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24223695Sdfr * SUCH DAMAGE.
25223695Sdfr *
26223695Sdfr * $FreeBSD: stable/11/stand/userboot/userboot.h 344413 2019-02-21 03:18:12Z kevans $
27223695Sdfr */
28223695Sdfr
29223695Sdfr/*
30223695Sdfr * USERBOOT interface versions
31223695Sdfr */
32223695Sdfr#define	USERBOOT_VERSION_1      1
33239073Sae#define	USERBOOT_VERSION_2      2
34242935Sneel#define	USERBOOT_VERSION_3      3
35223695Sdfr
36223695Sdfr/*
37296099Smarcel * Version 4 added more generic callbacks for setting up
38296099Smarcel * registers and descriptors. The callback structure is
39296099Smarcel * backward compatible (new callbacks have been added at
40296099Smarcel * the tail end).
41296099Smarcel */
42296099Smarcel#define	USERBOOT_VERSION_4      4
43296099Smarcel
44296099Smarcel/*
45344413Skevans * Version 5 added a callback for indicating that the guest
46344413Skevans * should be restarted with a different interpreter.  The callback
47344413Skevans * structure is still backward compatible.
48344413Skevans */
49344413Skevans#define	USERBOOT_VERSION_5      5
50344413Skevans
51344413Skevans/*
52223695Sdfr * Exit codes from the loader
53223695Sdfr */
54223695Sdfr#define	USERBOOT_EXIT_QUIT      1
55223695Sdfr#define	USERBOOT_EXIT_REBOOT    2
56223695Sdfr
57241164Saestruct loader_callbacks {
58223695Sdfr	/*
59223695Sdfr	 * Console i/o
60223695Sdfr	 */
61223695Sdfr
62223695Sdfr        /*
63223695Sdfr         * Wait until a key is pressed on the console and then return it
64223695Sdfr         */
65223695Sdfr	int		(*getc)(void *arg);
66223695Sdfr
67223695Sdfr        /*
68223695Sdfr         * Write the character ch to the console
69223695Sdfr         */
70223695Sdfr	void		(*putc)(void *arg, int ch);
71223695Sdfr
72223695Sdfr        /*
73223695Sdfr         * Return non-zero if a key can be read from the console
74223695Sdfr         */
75223695Sdfr	int		(*poll)(void *arg);
76223695Sdfr
77223695Sdfr	/*
78223695Sdfr	 * Host filesystem i/o
79223695Sdfr	 */
80223695Sdfr
81223695Sdfr        /*
82223695Sdfr         * Open a file in the host filesystem
83223695Sdfr         */
84223695Sdfr	int		(*open)(void *arg, const char *filename, void **h_return);
85223695Sdfr
86223695Sdfr        /*
87223695Sdfr         * Close a file
88223695Sdfr         */
89223695Sdfr	int		(*close)(void *arg, void *h);
90223695Sdfr
91223695Sdfr        /*
92223695Sdfr         * Return non-zero if the file is a directory
93223695Sdfr         */
94223695Sdfr	int		(*isdir)(void *arg, void *h);
95223695Sdfr
96223695Sdfr        /*
97223695Sdfr         * Read size bytes from a file. The number of bytes remaining
98223695Sdfr         * in dst after reading is returned in *resid_return
99223695Sdfr         */
100223695Sdfr	int		(*read)(void *arg, void *h, void *dst, size_t size,
101223695Sdfr            size_t *resid_return);
102223695Sdfr
103223695Sdfr        /*
104223695Sdfr         * Read an entry from a directory. The entry's inode number is
105223695Sdfr         * returned in *fileno_return, its type in *type_return and
106223695Sdfr         * the name length in *namelen_return. The name itself is
107223695Sdfr         * copied to the buffer name which must be at least PATH_MAX
108223695Sdfr         * in size.
109223695Sdfr         */
110223695Sdfr	int		(*readdir)(void *arg, void *h, uint32_t *fileno_return,
111223695Sdfr            uint8_t *type_return, size_t *namelen_return, char *name);
112223695Sdfr
113223695Sdfr        /*
114223695Sdfr         * Seek to a location within an open file
115223695Sdfr         */
116223695Sdfr	int		(*seek)(void *arg, void *h, uint64_t offset,
117223695Sdfr            int whence);
118223695Sdfr
119223695Sdfr        /*
120223695Sdfr         * Return some stat(2) related information about the file
121223695Sdfr         */
122223695Sdfr	int		(*stat)(void *arg, void *h, int *mode_return,
123223695Sdfr            int *uid_return, int *gid_return, uint64_t *size_return);
124223695Sdfr
125223695Sdfr	/*
126223695Sdfr	 * Disk image i/o
127223695Sdfr	 */
128223695Sdfr
129223695Sdfr        /*
130223695Sdfr         * Read from a disk image at the given offset
131223695Sdfr         */
132223695Sdfr	int		(*diskread)(void *arg, int unit, uint64_t offset,
133223695Sdfr            void *dst, size_t size, size_t *resid_return);
134223695Sdfr
135223695Sdfr	/*
136223695Sdfr	 * Guest virtual machine i/o
137223695Sdfr	 */
138223695Sdfr
139223695Sdfr        /*
140223695Sdfr         * Copy to the guest address space
141223695Sdfr         */
142223695Sdfr	int		(*copyin)(void *arg, const void *from,
143223695Sdfr            uint64_t to, size_t size);
144223695Sdfr
145223695Sdfr        /*
146223695Sdfr         * Copy from the guest address space
147223695Sdfr         */
148223695Sdfr	int		(*copyout)(void *arg, uint64_t from,
149223695Sdfr            void *to, size_t size);
150223695Sdfr
151223695Sdfr        /*
152223695Sdfr         * Set a guest register value
153223695Sdfr         */
154223695Sdfr	void		(*setreg)(void *arg, int, uint64_t);
155223695Sdfr
156223695Sdfr        /*
157223695Sdfr         * Set a guest MSR value
158223695Sdfr         */
159223695Sdfr	void		(*setmsr)(void *arg, int, uint64_t);
160223695Sdfr
161223695Sdfr        /*
162223695Sdfr         * Set a guest CR value
163223695Sdfr         */
164223695Sdfr	void		(*setcr)(void *arg, int, uint64_t);
165223695Sdfr
166223695Sdfr        /*
167223695Sdfr         * Set the guest GDT address
168223695Sdfr         */
169223695Sdfr        void            (*setgdt)(void *arg, uint64_t, size_t);
170223695Sdfr
171223695Sdfr        /*
172223695Sdfr         * Transfer control to the guest at the given address
173223695Sdfr         */
174223695Sdfr	void		(*exec)(void *arg, uint64_t pc);
175223695Sdfr
176223695Sdfr	/*
177223695Sdfr	 * Misc
178223695Sdfr	 */
179223695Sdfr
180223695Sdfr        /*
181223695Sdfr         * Sleep for usec microseconds
182223695Sdfr         */
183223695Sdfr	void		(*delay)(void *arg, int usec);
184223695Sdfr
185223695Sdfr        /*
186223695Sdfr         * Exit with the given exit code
187223695Sdfr         */
188223695Sdfr	void		(*exit)(void *arg, int v);
189223695Sdfr
190223695Sdfr        /*
191223695Sdfr         * Return guest physical memory map details
192223695Sdfr         */
193223695Sdfr	void		(*getmem)(void *arg, uint64_t *lowmem,
194223695Sdfr            uint64_t *highmem);
195242935Sneel
196239058Sae	/*
197239058Sae	 * ioctl interface to the disk device
198239058Sae	 */
199239058Sae	int		(*diskioctl)(void *arg, int unit, u_long cmd,
200239058Sae	    void *data);
201242935Sneel
202242935Sneel	/*
203242935Sneel	 * Returns an environment variable in the form "name=value".
204242935Sneel	 *
205242935Sneel	 * If there are no more variables that need to be set in the
206242935Sneel	 * loader environment then return NULL.
207242935Sneel	 *
208242935Sneel	 * 'num' is used as a handle for the callback to identify which
209242935Sneel	 * environment variable to return next. It will begin at 0 and
210242935Sneel	 * each invocation will add 1 to the previous value of 'num'.
211242935Sneel	 */
212329114Skevans	char *		(*getenv)(void *arg, int num);
213296099Smarcel
214296099Smarcel	/*
215296099Smarcel	 * Version 4 additions.
216296099Smarcel	 */
217296099Smarcel	int	(*vm_set_register)(void *arg, int vcpu, int reg, uint64_t val);
218296099Smarcel	int	(*vm_set_desc)(void *arg, int vcpu, int reg, uint64_t base,
219296099Smarcel	    u_int limit, u_int access);
220344413Skevans
221344413Skevans	/*
222344413Skevans	 * Version 5 addition.
223344413Skevans	 */
224344413Skevans	void	(*swap_interpreter)(void *arg, const char *interp);
225223695Sdfr};
226