boot.h 7.5 KB

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