/src/arch/x86_64/boot16/head.S

https://github.com/himikof/yallos · Assembly · 401 lines · 144 code · 86 blank · 171 comment · 0 complexity · 49449c3a367e2e570522510759342a99 MD5 · raw file

  1. /*
  2. ; KupOS kernel header |
  3. ;-----------------------------------+
  4. */
  5. .intel_syntax noprefix
  6. .include "macro.inc"
  7. .include "define.inc"
  8. .org GDT_IDT
  9. .code16
  10. .section .gdt, "a"
  11. /*
  12. ;------------------------------------------------+
  13. ; D A T A |
  14. ;------------------------------------------------+
  15. */
  16. ENTRY GDT
  17. null_desc: .quad 0
  18. code_descr: .byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0b10011010, 0b11001111 , 0x00
  19. data_descr: .byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0b10010010, 0b11001111 , 0x00
  20. video_descr: .byte 0xFF, 0xFF, 0x00, 0x80, 0x0b, 0b10010010, 0b11001111 , 0x00
  21. stack_descr: .byte 0xFF, 0xFF, 0xf0, 0xff, 0x3e, 0b10010010, 0b01001111 , 0x00
  22. .fill 8*256 - (. - GDT)
  23. GDT_sz = . - GDT - 1
  24. /*;--------------------------------134 */
  25. .section .idt, "aw"
  26. ENTRY IDT
  27. trap_int = 0b1000111100000000
  28. default_int = 0b1000111000000000
  29. cs_seg = 0x08
  30. /*
  31. ;------------;
  32. ; Exceptions ;
  33. ;------------;
  34. There they were...
  35. */
  36. /*
  37. ;------;
  38. ; IRQs ;
  39. ;------;
  40. */
  41. /* dwIDT int_20h, cs_seg, default_int
  42. dwIDT int_21h, cs_seg, default_int
  43. .rept 4
  44. dwIDT IRQ1_end, cs_seg, default_int
  45. .endr
  46. dwIDT int_26h, cs_seg, default_int
  47. dwIDT IRQ1_end, cs_seg, default_int
  48. .rept 8
  49. dwIDT IRQ2_end, cs_seg, default_int
  50. .endr
  51. */
  52. /*
  53. ;-------------;
  54. ; KERNEL INTs ;
  55. ;-------------;
  56. */
  57. /* dwIDT int_30h, cs_seg, default_int */ /* OUTPUT */
  58. /* dwIDT int_31h, cs_seg, default_int */ /* INPUT */
  59. /* dwIDT int_32h, cs_seg, default_int */ /* FLOPPY */
  60. /* dwIDT int_33h, cs_seg, default_int */ /* MEMORY */
  61. .fill 8*256 - (. - IDT)
  62. IDT_sz = . - IDT - 1
  63. /*
  64. ;---------------------------------------------------------------------------;
  65. ; H E A D S T A R T ;
  66. ;---------------------------------------------------------------------------;
  67. */
  68. .section .text.head, "ax"
  69. /* .org OS_HEAD */
  70. ENTRY boot_entry16
  71. /*
  72. ;--------------------------------------------------------;
  73. ; P r o t e c t e d M o d e ;
  74. ;--------------------------------------------------------;
  75. */
  76. cli
  77. in al, 0x70
  78. jmp .+2
  79. or al, 0x80 /* cli NMI */
  80. out 0x70, al
  81. jmp .+2
  82. in al, 0x92
  83. jmp .+2
  84. or al, 2 /* A20 - available */
  85. out 0x92, al
  86. jmp .+2
  87. /* rePrograming controlers */
  88. mov al, 0x11
  89. out 0x20, al
  90. jmp .+2
  91. out 0xA0, al
  92. jmp .+2
  93. mov al, 0x20
  94. out 0x21, al
  95. jmp .+2
  96. mov al, 0x28
  97. out 0xA1, al
  98. jmp .+2
  99. mov al, 4
  100. out 0x21, al
  101. jmp .+2
  102. mov al, 2
  103. out 0xA1, al
  104. jmp .+2
  105. mov al, 1
  106. out 0x21, al
  107. jmp .+2
  108. out 0xA1, al
  109. jmp .+2
  110. mov al, -1
  111. out 0x21, al
  112. jmp .+2
  113. out 0xA1, al
  114. jmp .+2
  115. lgdt [GDTR]
  116. lidt [IDTR]
  117. mov eax, cr0
  118. or al, 1
  119. mov cr0, eax
  120. jmp 8:offset prot_mode
  121. prot_mode:
  122. .code32
  123. mov eax, 0b10000
  124. mov ds, ax
  125. mov es, ax
  126. mov eax, 0b11000
  127. mov gs, ax
  128. lss esp, [_SYS_STACK]
  129. /*
  130. ;-------------------------------------------+
  131. ; C O P Y M A I N |
  132. ;-------------------------------------------+
  133. */
  134. /*
  135. .comm _end, 4
  136. .comm _system_text, 4
  137. mov esi, _main_com_
  138. mov edi, _system_text /* memory 0x03000 - 0x13000 (64kb) = memory for floppy DMA *//*
  139. mov ecx, (_end - _system_text) / 4
  140. rep movsd /* main is 0x13000 and we copy it there *//*
  141. */
  142. /*
  143. ;-------------------------------------------+
  144. ; P a g i n g - ;-(((((((((((((((((((( |
  145. ;-------------------------------------------+
  146. */
  147. mov eax, 0x1407
  148. mov ecx, 1023 /* Catalog */
  149. xor edi, edi
  150. stosd
  151. add eax, 0x3ff000
  152. /* of */
  153. .fill_catalog:
  154. stosd /* Tables of Pages */
  155. add eax, 0x1000
  156. loop .fill_catalog
  157. xor eax, eax
  158. mov al, 0x07
  159. mov ecx, 1024
  160. /* Fill */
  161. .fill: /* table */
  162. stosd /* of */
  163. add eax, 0x1000 /* Pages 1 */
  164. loop .fill
  165. lea edi, [eax-7]
  166. mov ecx, 1024*1023 /* Fill */
  167. .fill2: /* tables */
  168. stosd /* of */
  169. add eax, 0x1000 /* Pages 2-1024 */
  170. loop .fill2
  171. /* -- stop filling -- */
  172. xor eax, eax
  173. mov cr3, eax
  174. mov eax, cr0
  175. bts eax, 0x1f /* and now setup paging =))) fuf */
  176. mov cr0, eax
  177. /*
  178. ;-------------------------------------------+
  179. ; I n t e r r u p t i o n =) |
  180. ;-------------------------------------------+
  181. */
  182. /* mov byte [num_in_key_buf], 0 */ /* for keyboard */
  183. xor al, al
  184. out 0x21, al
  185. jmp .+2 /* controlers sti */
  186. out 0xA1, al
  187. jmp .+2
  188. in al, 0x70
  189. jmp .+2
  190. and al, 0x7f /* NMI sti */
  191. out 0x70, al
  192. jmp .+2
  193. call fill_idt
  194. /*sti*/ /* It's not safe yet */
  195. /*
  196. ;--------------------------------+
  197. ; Set form of cursor | why here?
  198. ;--------------------------------+
  199. */
  200. mov dx, 0x03d4
  201. mov ax, 0x000A /* Select reg #10 and reg #10 <- 0 (Low line of cursor) */
  202. out dx, ax
  203. jmp .+2
  204. mov ax, 0x0D0B /* Select reg #11 and reg #11 <- 13 (High line of cursor) */
  205. out dx, ax
  206. jmp .+2
  207. .comm _kernel_main, 4
  208. jmp _kernel_main /* should never return */
  209. fill_idt:
  210. mov ecx, 0x100
  211. mov ebx, offset IDT
  212. mov edx, offset ignore_interrupt
  213. mov eax, 0x00080000
  214. mov ax, dx
  215. mov dx, 0x008E
  216. 1:
  217. mov dword ptr [ebx], eax
  218. mov dword ptr [ebx+4], edx
  219. add ebx, 8
  220. loop 1b
  221. ret
  222. ignore_interrupt: /* dummy handler */
  223. iret
  224. /*
  225. ;-----------------------------------------------+
  226. ; DATA |
  227. ;-----------------------------------------------+
  228. */
  229. .section .initdata, "a"
  230. GDTR: .word GDT_sz
  231. .long GDT
  232. IDTR: .word IDT_sz
  233. .long IDT
  234. _SYS_STACK: .long SYS_STACK_ESP
  235. .word 0b100000
  236. /*------------------------------------------*/
  237. /*.section .buffer, "a"
  238. .org MAIN_COMMANDS*/
  239. /* .include "main.S" */
  240. /*
  241. E N D O F H E A D
  242. */
  243. /* OLD STRUCTURES */
  244. /*
  245. ;-------------------------------------------------------------------------------------------------------------------+
  246. ; IRQ - Timer, Key etc |
  247. ;-------------------------------------------------------------------------------------------------------------------+
  248. */
  249. /* IRQ1_end: /* exit IRQ interruption *//*
  250. push eax
  251. mov al, 0x20
  252. out 0x20, al
  253. jmp .+2
  254. pop eax
  255. iret
  256. IRQ2_end: /* exit IRQ interruption *//*
  257. push eax
  258. mov al, 0x20
  259. out 0xA0, al
  260. jmp .+2
  261. pop eax
  262. iret
  263. */
  264. /*
  265. ;------------------+
  266. ; IRQ0 - Timer |
  267. ;------------------+
  268. */
  269. /*.include "IRQ0.S"*/
  270. /*
  271. ;------------------+
  272. ; IRQ1 - Keyboard |
  273. ;------------------+
  274. */
  275. /*.include "IRQ1.S"*/
  276. /*
  277. ;---------------+
  278. ; IRQ6 - Floppy |
  279. ;---------------+
  280. */
  281. /*.include "IRQ6.S"*/
  282. /*
  283. ;-------------------------------------------------------------------------------------------------------------------+
  284. ; INT 30h - OUTPUT TO CONSOLE |
  285. ;-------------------------------------------------------------------------------------------------------------------+
  286. */
  287. /*.include "OUTPUT.S"*/
  288. /*
  289. ;-------------------------------------------------------------------------------------------------------------------+
  290. ; INT 31h - INPUT TO CONSOLE |
  291. ;-------------------------------------------------------------------------------------------------------------------+
  292. */
  293. /*.include "INPUT.S"*/
  294. /*
  295. ;-------------------------------------------------------------------------------------------------------------------+
  296. ; INT 32h - F L O P P Y |
  297. ;-------------------------------------------------------------------------------------------------------------------+
  298. */
  299. /*.include "FLOPPY.S"*/
  300. /*
  301. ;-------------------------------------------------------------------------------------------------------------------+
  302. ; INT 33h - MEMORY |
  303. ;-------------------------------------------------------------------------------------------------------------------+
  304. */
  305. /*.include "MEMORY.S"*/
  306. /* .long 0 */ /* align for copy by dword */