boot.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. *
  6. * This file is part of the Linux kernel, and is made available under
  7. * the terms of the GNU General Public License version 2.
  8. *
  9. * ----------------------------------------------------------------------- */
  10. /*
  11. * Header file for the real-mode kernel code
  12. */
  13. #ifndef BOOT_BOOT_H
  14. #define BOOT_BOOT_H
  15. #define STACK_SIZE 512 /* Minimum number of bytes for stack */
  16. #ifndef __ASSEMBLY__
  17. #include <stdarg.h>
  18. #include <linux/types.h>
  19. #include <linux/edd.h>
  20. #include <asm/boot.h>
  21. #include <asm/setup.h>
  22. #include "bitops.h"
  23. #include <asm/cpufeature.h>
  24. /* Useful macros */
  25. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  26. #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
  27. extern struct setup_header hdr;
  28. extern struct boot_params boot_params;
  29. /* Basic port I/O */
  30. static inline void outb(u8 v, u16 port)
  31. {
  32. asm volatile("outb %0,%1" : : "a" (v), "dN" (port));
  33. }
  34. static inline u8 inb(u16 port)
  35. {
  36. u8 v;
  37. asm volatile("inb %1,%0" : "=a" (v) : "dN" (port));
  38. return v;
  39. }
  40. static inline void outw(u16 v, u16 port)
  41. {
  42. asm volatile("outw %0,%1" : : "a" (v), "dN" (port));
  43. }
  44. static inline u16 inw(u16 port)
  45. {
  46. u16 v;
  47. asm volatile("inw %1,%0" : "=a" (v) : "dN" (port));
  48. return v;
  49. }
  50. static inline void outl(u32 v, u16 port)
  51. {
  52. asm volatile("outl %0,%1" : : "a" (v), "dN" (port));
  53. }
  54. static inline u32 inl(u32 port)
  55. {
  56. u32 v;
  57. asm volatile("inl %1,%0" : "=a" (v) : "dN" (port));
  58. return v;
  59. }
  60. static inline void io_delay(void)
  61. {
  62. const u16 DELAY_PORT = 0x80;
  63. asm volatile("outb %%al,%0" : : "dN" (DELAY_PORT));
  64. }
  65. /* These functions are used to reference data in other segments. */
  66. static inline u16 ds(void)
  67. {
  68. u16 seg;
  69. asm("movw %%ds,%0" : "=rm" (seg));
  70. return seg;
  71. }
  72. static inline void set_fs(u16 seg)
  73. {
  74. asm volatile("movw %0,%%fs" : : "rm" (seg));
  75. }
  76. static inline u16 fs(void)
  77. {
  78. u16 seg;
  79. asm volatile("movw %%fs,%0" : "=rm" (seg));
  80. return seg;
  81. }
  82. static inline void set_gs(u16 seg)
  83. {
  84. asm volatile("movw %0,%%gs" : : "rm" (seg));
  85. }
  86. static inline u16 gs(void)
  87. {
  88. u16 seg;
  89. asm volatile("movw %%gs,%0" : "=rm" (seg));
  90. return seg;
  91. }
  92. typedef unsigned int addr_t;
  93. static inline u8 rdfs8(addr_t addr)
  94. {
  95. u8 v;
  96. asm volatile("movb %%fs:%1,%0" : "=q" (v) : "m" (*(u8 *)addr));
  97. return v;
  98. }
  99. static inline u16 rdfs16(addr_t addr)
  100. {
  101. u16 v;
  102. asm volatile("movw %%fs:%1,%0" : "=r" (v) : "m" (*(u16 *)addr));
  103. return v;
  104. }
  105. static inline u32 rdfs32(addr_t addr)
  106. {
  107. u32 v;
  108. asm volatile("movl %%fs:%1,%0" : "=r" (v) : "m" (*(u32 *)addr));
  109. return v;
  110. }
  111. static inline void wrfs8(u8 v, addr_t addr)
  112. {
  113. asm volatile("movb %1,%%fs:%0" : "+m" (*(u8 *)addr) : "qi" (v));
  114. }
  115. static inline void wrfs16(u16 v, addr_t addr)
  116. {
  117. asm volatile("movw %1,%%fs:%0" : "+m" (*(u16 *)addr) : "ri" (v));
  118. }
  119. static inline void wrfs32(u32 v, addr_t addr)
  120. {
  121. asm volatile("movl %1,%%fs:%0" : "+m" (*(u32 *)addr) : "ri" (v));
  122. }
  123. static inline u8 rdgs8(addr_t addr)
  124. {
  125. u8 v;
  126. asm volatile("movb %%gs:%1,%0" : "=q" (v) : "m" (*(u8 *)addr));
  127. return v;
  128. }
  129. static inline u16 rdgs16(addr_t addr)
  130. {
  131. u16 v;
  132. asm volatile("movw %%gs:%1,%0" : "=r" (v) : "m" (*(u16 *)addr));
  133. return v;
  134. }
  135. static inline u32 rdgs32(addr_t addr)
  136. {
  137. u32 v;
  138. asm volatile("movl %%gs:%1,%0" : "=r" (v) : "m" (*(u32 *)addr));
  139. return v;
  140. }
  141. static inline void wrgs8(u8 v, addr_t addr)
  142. {
  143. asm volatile("movb %1,%%gs:%0" : "+m" (*(u8 *)addr) : "qi" (v));
  144. }
  145. static inline void wrgs16(u16 v, addr_t addr)
  146. {
  147. asm volatile("movw %1,%%gs:%0" : "+m" (*(u16 *)addr) : "ri" (v));
  148. }
  149. static inline void wrgs32(u32 v, addr_t addr)
  150. {
  151. asm volatile("movl %1,%%gs:%0" : "+m" (*(u32 *)addr) : "ri" (v));
  152. }
  153. /* Note: these only return true/false, not a signed return value! */
  154. static inline int memcmp(const void *s1, const void *s2, size_t len)
  155. {
  156. u8 diff;
  157. asm("repe; cmpsb; setnz %0"
  158. : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
  159. return diff;
  160. }
  161. static inline int memcmp_fs(const void *s1, addr_t s2, size_t len)
  162. {
  163. u8 diff;
  164. asm volatile("fs; repe; cmpsb; setnz %0"
  165. : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
  166. return diff;
  167. }
  168. static inline int memcmp_gs(const void *s1, addr_t s2, size_t len)
  169. {
  170. u8 diff;
  171. asm volatile("gs; repe; cmpsb; setnz %0"
  172. : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
  173. return diff;
  174. }
  175. static inline int isdigit(int ch)
  176. {
  177. return (ch >= '0') && (ch <= '9');
  178. }
  179. /* Heap -- available for dynamic lists. */
  180. extern char _end[];
  181. extern char *HEAP;
  182. extern char *heap_end;
  183. #define RESET_HEAP() ((void *)( HEAP = _end ))
  184. static inline char *__get_heap(size_t s, size_t a, size_t n)
  185. {
  186. char *tmp;
  187. HEAP = (char *)(((size_t)HEAP+(a-1)) & ~(a-1));
  188. tmp = HEAP;
  189. HEAP += s*n;
  190. return tmp;
  191. }
  192. #define GET_HEAP(type, n) \
  193. ((type *)__get_heap(sizeof(type),__alignof__(type),(n)))
  194. static inline bool heap_free(size_t n)
  195. {
  196. return (int)(heap_end-HEAP) >= (int)n;
  197. }
  198. /* copy.S */
  199. void copy_to_fs(addr_t dst, void *src, size_t len);
  200. void *copy_from_fs(void *dst, addr_t src, size_t len);
  201. void copy_to_gs(addr_t dst, void *src, size_t len);
  202. void *copy_from_gs(void *dst, addr_t src, size_t len);
  203. void *memcpy(void *dst, void *src, size_t len);
  204. void *memset(void *dst, int c, size_t len);
  205. #define memcpy(d,s,l) __builtin_memcpy(d,s,l)
  206. #define memset(d,c,l) __builtin_memset(d,c,l)
  207. /* a20.c */
  208. int enable_a20(void);
  209. /* apm.c */
  210. int query_apm_bios(void);
  211. /* cmdline.c */
  212. int cmdline_find_option(const char *option, char *buffer, int bufsize);
  213. int cmdline_find_option_bool(const char *option);
  214. /* cpu.c, cpucheck.c */
  215. struct cpu_features {
  216. int level; /* Family, or 64 for x86-64 */
  217. int model;
  218. u32 flags[NCAPINTS];
  219. };
  220. extern struct cpu_features cpu;
  221. int check_cpu(int *cpu_level_ptr, int *req_level_ptr, u32 **err_flags_ptr);
  222. int validate_cpu(void);
  223. /* edd.c */
  224. void query_edd(void);
  225. /* header.S */
  226. void __attribute__((noreturn)) die(void);
  227. /* mca.c */
  228. int query_mca(void);
  229. /* memory.c */
  230. int detect_memory(void);
  231. /* pm.c */
  232. void __attribute__((noreturn)) go_to_protected_mode(void);
  233. /* pmjump.S */
  234. void __attribute__((noreturn))
  235. protected_mode_jump(u32 entrypoint, u32 bootparams);
  236. /* printf.c */
  237. int sprintf(char *buf, const char *fmt, ...);
  238. int vsprintf(char *buf, const char *fmt, va_list args);
  239. int printf(const char *fmt, ...);
  240. /* string.c */
  241. int strcmp(const char *str1, const char *str2);
  242. size_t strnlen(const char *s, size_t maxlen);
  243. unsigned int atou(const char *s);
  244. /* tty.c */
  245. void puts(const char *);
  246. void putchar(int);
  247. int getchar(void);
  248. void kbd_flush(void);
  249. int getchar_timeout(void);
  250. /* video.c */
  251. void set_video(void);
  252. /* video-mode.c */
  253. int set_mode(u16 mode);
  254. int mode_defined(u16 mode);
  255. void probe_cards(int unsafe);
  256. /* video-vesa.c */
  257. void vesa_store_edid(void);
  258. #endif /* __ASSEMBLY__ */
  259. #endif /* BOOT_BOOT_H */