1/* SPDX-License-Identifier: GPL-2.0-only */
2/* -*- linux-c -*- ------------------------------------------------------- *
3 *
4 *   Copyright (C) 1991, 1992 Linus Torvalds
5 *   Copyright 2007 rPath, Inc. - All Rights Reserved
6 *   Copyright 2009 Intel Corporation; author H. Peter Anvin
7 *
8 * ----------------------------------------------------------------------- */
9
10/*
11 * Header file for the real-mode kernel code
12 */
13
14#ifndef BOOT_BOOT_H
15#define BOOT_BOOT_H
16
17#define STACK_SIZE	1024	/* Minimum number of bytes for stack */
18
19#ifndef __ASSEMBLY__
20
21#include <linux/stdarg.h>
22#include <linux/types.h>
23#include <linux/edd.h>
24#include <asm/setup.h>
25#include <asm/asm.h>
26#include "bitops.h"
27#include "ctype.h"
28#include "cpuflags.h"
29#include "io.h"
30
31/* Useful macros */
32#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
33
34extern struct setup_header hdr;
35extern struct boot_params boot_params;
36
37#define cpu_relax()	asm volatile("rep; nop")
38
39static inline void io_delay(void)
40{
41	const u16 DELAY_PORT = 0x80;
42	outb(0, DELAY_PORT);
43}
44
45/* These functions are used to reference data in other segments. */
46
47static inline u16 ds(void)
48{
49	u16 seg;
50	asm("movw %%ds,%0" : "=rm" (seg));
51	return seg;
52}
53
54static inline void set_fs(u16 seg)
55{
56	asm volatile("movw %0,%%fs" : : "rm" (seg));
57}
58static inline u16 fs(void)
59{
60	u16 seg;
61	asm volatile("movw %%fs,%0" : "=rm" (seg));
62	return seg;
63}
64
65static inline void set_gs(u16 seg)
66{
67	asm volatile("movw %0,%%gs" : : "rm" (seg));
68}
69static inline u16 gs(void)
70{
71	u16 seg;
72	asm volatile("movw %%gs,%0" : "=rm" (seg));
73	return seg;
74}
75
76typedef unsigned int addr_t;
77
78static inline u8 rdfs8(addr_t addr)
79{
80	u8 *ptr = (u8 *)absolute_pointer(addr);
81	u8 v;
82	asm volatile("movb %%fs:%1,%0" : "=q" (v) : "m" (*ptr));
83	return v;
84}
85static inline u16 rdfs16(addr_t addr)
86{
87	u16 *ptr = (u16 *)absolute_pointer(addr);
88	u16 v;
89	asm volatile("movw %%fs:%1,%0" : "=r" (v) : "m" (*ptr));
90	return v;
91}
92static inline u32 rdfs32(addr_t addr)
93{
94	u32 *ptr = (u32 *)absolute_pointer(addr);
95	u32 v;
96	asm volatile("movl %%fs:%1,%0" : "=r" (v) : "m" (*ptr));
97	return v;
98}
99
100static inline void wrfs8(u8 v, addr_t addr)
101{
102	u8 *ptr = (u8 *)absolute_pointer(addr);
103	asm volatile("movb %1,%%fs:%0" : "+m" (*ptr) : "qi" (v));
104}
105static inline void wrfs16(u16 v, addr_t addr)
106{
107	u16 *ptr = (u16 *)absolute_pointer(addr);
108	asm volatile("movw %1,%%fs:%0" : "+m" (*ptr) : "ri" (v));
109}
110static inline void wrfs32(u32 v, addr_t addr)
111{
112	u32 *ptr = (u32 *)absolute_pointer(addr);
113	asm volatile("movl %1,%%fs:%0" : "+m" (*ptr) : "ri" (v));
114}
115
116static inline u8 rdgs8(addr_t addr)
117{
118	u8 *ptr = (u8 *)absolute_pointer(addr);
119	u8 v;
120	asm volatile("movb %%gs:%1,%0" : "=q" (v) : "m" (*ptr));
121	return v;
122}
123static inline u16 rdgs16(addr_t addr)
124{
125	u16 *ptr = (u16 *)absolute_pointer(addr);
126	u16 v;
127	asm volatile("movw %%gs:%1,%0" : "=r" (v) : "m" (*ptr));
128	return v;
129}
130static inline u32 rdgs32(addr_t addr)
131{
132	u32 *ptr = (u32 *)absolute_pointer(addr);
133	u32 v;
134	asm volatile("movl %%gs:%1,%0" : "=r" (v) : "m" (*ptr));
135	return v;
136}
137
138static inline void wrgs8(u8 v, addr_t addr)
139{
140	u8 *ptr = (u8 *)absolute_pointer(addr);
141	asm volatile("movb %1,%%gs:%0" : "+m" (*ptr) : "qi" (v));
142}
143static inline void wrgs16(u16 v, addr_t addr)
144{
145	u16 *ptr = (u16 *)absolute_pointer(addr);
146	asm volatile("movw %1,%%gs:%0" : "+m" (*ptr) : "ri" (v));
147}
148static inline void wrgs32(u32 v, addr_t addr)
149{
150	u32 *ptr = (u32 *)absolute_pointer(addr);
151	asm volatile("movl %1,%%gs:%0" : "+m" (*ptr) : "ri" (v));
152}
153
154/* Note: these only return true/false, not a signed return value! */
155static inline bool memcmp_fs(const void *s1, addr_t s2, size_t len)
156{
157	bool diff;
158	asm volatile("fs; repe; cmpsb" CC_SET(nz)
159		     : CC_OUT(nz) (diff), "+D" (s1), "+S" (s2), "+c" (len));
160	return diff;
161}
162static inline bool memcmp_gs(const void *s1, addr_t s2, size_t len)
163{
164	bool diff;
165	asm volatile("gs; repe; cmpsb" CC_SET(nz)
166		     : CC_OUT(nz) (diff), "+D" (s1), "+S" (s2), "+c" (len));
167	return diff;
168}
169
170/* Heap -- available for dynamic lists. */
171extern char _end[];
172extern char *HEAP;
173extern char *heap_end;
174#define RESET_HEAP() ((void *)( HEAP = _end ))
175static inline char *__get_heap(size_t s, size_t a, size_t n)
176{
177	char *tmp;
178
179	HEAP = (char *)(((size_t)HEAP+(a-1)) & ~(a-1));
180	tmp = HEAP;
181	HEAP += s*n;
182	return tmp;
183}
184#define GET_HEAP(type, n) \
185	((type *)__get_heap(sizeof(type),__alignof__(type),(n)))
186
187static inline bool heap_free(size_t n)
188{
189	return (int)(heap_end-HEAP) >= (int)n;
190}
191
192/* copy.S */
193
194void copy_to_fs(addr_t dst, void *src, size_t len);
195void *copy_from_fs(void *dst, addr_t src, size_t len);
196void copy_to_gs(addr_t dst, void *src, size_t len);
197void *copy_from_gs(void *dst, addr_t src, size_t len);
198
199/* a20.c */
200int enable_a20(void);
201
202/* apm.c */
203int query_apm_bios(void);
204
205/* bioscall.c */
206struct biosregs {
207	union {
208		struct {
209			u32 edi;
210			u32 esi;
211			u32 ebp;
212			u32 _esp;
213			u32 ebx;
214			u32 edx;
215			u32 ecx;
216			u32 eax;
217			u32 _fsgs;
218			u32 _dses;
219			u32 eflags;
220		};
221		struct {
222			u16 di, hdi;
223			u16 si, hsi;
224			u16 bp, hbp;
225			u16 _sp, _hsp;
226			u16 bx, hbx;
227			u16 dx, hdx;
228			u16 cx, hcx;
229			u16 ax, hax;
230			u16 gs, fs;
231			u16 es, ds;
232			u16 flags, hflags;
233		};
234		struct {
235			u8 dil, dih, edi2, edi3;
236			u8 sil, sih, esi2, esi3;
237			u8 bpl, bph, ebp2, ebp3;
238			u8 _spl, _sph, _esp2, _esp3;
239			u8 bl, bh, ebx2, ebx3;
240			u8 dl, dh, edx2, edx3;
241			u8 cl, ch, ecx2, ecx3;
242			u8 al, ah, eax2, eax3;
243		};
244	};
245};
246void intcall(u8 int_no, const struct biosregs *ireg, struct biosregs *oreg);
247
248/* cmdline.c */
249int __cmdline_find_option(unsigned long cmdline_ptr, const char *option, char *buffer, int bufsize);
250int __cmdline_find_option_bool(unsigned long cmdline_ptr, const char *option);
251static inline int cmdline_find_option(const char *option, char *buffer, int bufsize)
252{
253	unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
254
255	if (cmd_line_ptr >= 0x100000)
256		return -1;      /* inaccessible */
257
258	return __cmdline_find_option(cmd_line_ptr, option, buffer, bufsize);
259}
260
261static inline int cmdline_find_option_bool(const char *option)
262{
263	unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
264
265	if (cmd_line_ptr >= 0x100000)
266		return -1;      /* inaccessible */
267
268	return __cmdline_find_option_bool(cmd_line_ptr, option);
269}
270
271/* cpu.c, cpucheck.c */
272int check_cpu(int *cpu_level_ptr, int *req_level_ptr, u32 **err_flags_ptr);
273int check_knl_erratum(void);
274int validate_cpu(void);
275
276/* early_serial_console.c */
277extern int early_serial_base;
278void console_init(void);
279
280/* edd.c */
281void query_edd(void);
282
283/* header.S */
284void __attribute__((noreturn)) die(void);
285
286/* memory.c */
287void detect_memory(void);
288
289/* pm.c */
290void __attribute__((noreturn)) go_to_protected_mode(void);
291
292/* pmjump.S */
293void __attribute__((noreturn))
294	protected_mode_jump(u32 entrypoint, u32 bootparams);
295
296/* printf.c */
297int sprintf(char *buf, const char *fmt, ...);
298int vsprintf(char *buf, const char *fmt, va_list args);
299int printf(const char *fmt, ...);
300
301/* regs.c */
302void initregs(struct biosregs *regs);
303
304/* string.c */
305int strcmp(const char *str1, const char *str2);
306int strncmp(const char *cs, const char *ct, size_t count);
307size_t strnlen(const char *s, size_t maxlen);
308unsigned int atou(const char *s);
309unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base);
310size_t strlen(const char *s);
311char *strchr(const char *s, int c);
312
313/* tty.c */
314void puts(const char *);
315void putchar(int);
316int getchar(void);
317void kbd_flush(void);
318int getchar_timeout(void);
319
320/* video.c */
321void set_video(void);
322
323/* video-mode.c */
324int set_mode(u16 mode);
325int mode_defined(u16 mode);
326void probe_cards(int unsafe);
327
328/* video-vesa.c */
329void vesa_store_edid(void);
330
331#endif /* __ASSEMBLY__ */
332
333#endif /* BOOT_BOOT_H */
334