boot.h 6.5 KB

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