boot.h 6.3 KB

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