Advertisement
MichaelPetch

SO78689448-patch1.diff

Jul 2nd, 2024 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.50 KB | None | 0 0
  1. diff --git a/source/kernel/assembly/bios.asm b/source/kernel/assembly/bios.asm
  2. index 8a3a580..9cf3fc7 100644
  3. --- a/source/kernel/assembly/bios.asm
  4. +++ b/source/kernel/assembly/bios.asm
  5. @@ -26,24 +26,29 @@ section .text
  6.  
  7. ; 32 bit protected mode
  8. BIOS32_START:use32
  9. + pushf
  10. pusha
  11. - ; save current esp to edx
  12. - mov edx, esp
  13. +
  14. + ; Save the original IDTR and GDTR onto the stack
  15. + sub esp, 32
  16. + sidt [esp]
  17. + sgdt [esp+16]
  18. +
  19. + ; save current protected mode esp
  20. + mov [REBASE_ADDRESS(prot_esp)], esp
  21. +
  22. ; jumping to 16 bit protected mode
  23. ; disable interrupts
  24. cli
  25.  
  26. -
  27. ; Turn off paging
  28. - mov cr0, eax
  29. - and eax, DISABLE_PAGING
  30. mov eax, cr0
  31. + and eax, DISABLE_PAGING
  32. + mov cr0, eax
  33.  
  34. - ; Executing this corrupts CR3 (page table index) so save it in EBX
  35. - xor ecx, ecx
  36. - mov ebx, cr3
  37. - mov cr3, ecx
  38. -
  39. + ; Flush TLB
  40. + mov eax, cr3
  41. + mov cr3, eax
  42.  
  43. ; Load GDT
  44. lgdt [REBASE_ADDRESS(bios32_gdt_ptr)]
  45. @@ -97,11 +102,11 @@ __real_mode_16:use16
  46. push cx
  47. pushf
  48. ; get current stack pointer & save it to current_esp
  49. - mov ax, sp
  50. - mov edi, current_esp
  51. - stosw
  52. + mov eax, esp
  53. + mov edi, REBASE_ADDRESS(current_esp)
  54. + stosd
  55. ; load our custom registers context
  56. - mov esp, REBASE_ADDRESS(bios32_in_reg16_ptr)
  57. + mov sp, REBASE_ADDRESS(bios32_in_reg16_ptr)
  58. ; only use some general register from the given context
  59. popa
  60. ; set a new stack for bios interrupt
  61. @@ -129,9 +134,9 @@ bios32_int_number_ptr: ; will be bios interrupt number passed
  62. push cx
  63. pusha
  64. ; restore the current_esp to continue
  65. - mov esi, current_esp
  66. - lodsw
  67. - mov sp, ax
  68. + mov esi, REBASE_ADDRESS(current_esp)
  69. + lodsd
  70. + mov esp, eax
  71. ; restore all current context, all general, segment registers, flags
  72. popf
  73. pop cx
  74. @@ -148,8 +153,9 @@ bios32_int_number_ptr: ; will be bios interrupt number passed
  75.  
  76. ; jumping to 32 bit protected mode
  77. ; set bit 0 in cr0 to 1
  78. + cli
  79. mov eax, cr0
  80. - inc eax
  81. + or eax, ENABLE_PAGING
  82. mov cr0, eax
  83. jmp 0x08:REBASE_ADDRESS(__protected_mode_32)
  84.  
  85. @@ -162,29 +168,26 @@ __protected_mode_32:use32
  86. mov gs, ax
  87. mov ss, ax
  88.  
  89. - ; Restore CR3
  90. - mov cr3, ebx
  91. -
  92. - ; Enable paging
  93. - mov ecx, cr0
  94. - or ecx, ENABLE_PAGING
  95. - mov cr0, ecx
  96. -
  97. ; Restore ESP
  98. - mov esp, edx
  99. + mov esp, [REBASE_ADDRESS(prot_esp)]
  100.  
  101. - sti
  102. + ; Reload the original IDTR and GDTR from the stack
  103. + lidt [esp]
  104. + lgdt [esp+16]
  105. + add esp, 32
  106. +
  107. + ; Restore all registers
  108. popa
  109. +
  110. + ; Restore flags including Interrupt flag state
  111. + popf
  112. ret
  113.  
  114. +align 4
  115.  
  116. -__padding:
  117. - db 0x0
  118. - db 0x0
  119. - db 0x0
  120. bios32_gdt_entries:
  121. ; 8 gdt entries
  122. - resb 64
  123. + TIMES 8 dq 0
  124. bios32_gdt_ptr:
  125. dd 0x00000000
  126. dd 0x00000000
  127. @@ -192,7 +195,7 @@ bios32_idt_ptr:
  128. dd 0x00000000
  129. dd 0x00000000
  130. bios32_in_reg16_ptr:
  131. - resw 14
  132. + TIMES 14 dw 0
  133. bios32_out_reg16_ptr:
  134. dd 0xaaaaaaaa
  135. dd 0xaaaaaaaa
  136. @@ -202,6 +205,8 @@ bios32_out_reg16_ptr:
  137. dd 0xaaaaaaaa
  138. dd 0xaaaaaaaa
  139. current_esp:
  140. - dw 0x0000
  141. + dd 0x0000
  142. +prot_esp:
  143. + dd 0x0000
  144.  
  145. -BIOS32_END:
  146. \ No newline at end of file
  147. +BIOS32_END:
  148. diff --git a/source/kernel/bios32.c b/source/kernel/bios32.c
  149. index 811c432..ec42146 100644
  150. --- a/source/kernel/bios32.c
  151. +++ b/source/kernel/bios32.c
  152. @@ -35,31 +35,27 @@ void bios32_call(uint8_t interrupt, REGISTERS_16 *in, REGISTERS_16 *out) {
  153. // Update the base address of the GDT entries, starting from 0x7C00.
  154. realModeGDT.base_addr = (uint32_t)REBASE_ADDRESS((&bios32_gdt_entries));
  155.  
  156. - // Copy the real mode GDT and IDT to their respective pointers.
  157. - memcpy(&bios32_gdt_ptr, &realModeGDT, sizeof(idtPtr_t));
  158. - memcpy(&bios32_idt_ptr, &realModeIDT, sizeof(idtPtr_t));
  159. -
  160. - // Copy the in registers to their pointers.
  161. - memcpy(&bios32_in_reg16_ptr, in, sizeof(REGISTERS_16));
  162. -
  163. - // Get the in registers' address.
  164. - void *in_reg16_address = REBASE_ADDRESS(&bios32_in_reg16_ptr);
  165. -
  166. - // Copy the BIOS interrupt number to its respective pointer.
  167. - memcpy(&bios32_int_number_ptr, &interrupt, sizeof(uint8_t));
  168. -
  169. + // Copy the real mode GDT and IDT to their respective pointers.
  170. + memcpy(&bios32_gdt_ptr, &realModeGDT, sizeof(idtPtr_t));
  171. + memcpy(&bios32_idt_ptr, &realModeIDT, sizeof(idtPtr_t));
  172. +
  173. + // Copy the in registers to their pointers.
  174. + memcpy(&bios32_in_reg16_ptr, in, sizeof(REGISTERS_16));
  175. +
  176. + // Get the in registers' address.
  177. + void *in_reg16_address = REBASE_ADDRESS(&bios32_in_reg16_ptr);
  178. +
  179. + // Copy the BIOS interrupt number to its respective pointer.
  180. + memcpy(&bios32_int_number_ptr, &interrupt, sizeof(uint8_t));
  181. +
  182. // Copy the bios32 code to a new address.
  183. uint32_t size = (uint32_t)BIOS32_END - (uint32_t)BIOS32_START;
  184. memcpy(newCodeBase, BIOS32_START, size);
  185.  
  186. - // Start executing the BIOS32 code.
  187. - bios32_execute();
  188. -
  189. - // Copy the output registers to the out ptr.
  190. - in_reg16_address = REBASE_ADDRESS(&bios32_out_reg16_ptr);
  191. - memcpy(out, in_reg16_address, sizeof(REGISTERS_16));
  192. -
  193. - // Reinitialize GDT and IDT
  194. - gdtInit();
  195. - idtInit();
  196. -}
  197. \ No newline at end of file
  198. + // Start executing the BIOS32 code.
  199. + bios32_execute();
  200. +
  201. + // Copy the output registers to the out ptr.
  202. + in_reg16_address = REBASE_ADDRESS(&bios32_out_reg16_ptr);
  203. + memcpy(out, in_reg16_address, sizeof(REGISTERS_16));
  204. +}
  205. diff --git a/source/kernel/gdt.c b/source/kernel/gdt.c
  206. index f64818d..6b857a1 100644
  207. --- a/source/kernel/gdt.c
  208. +++ b/source/kernel/gdt.c
  209. @@ -30,19 +30,19 @@ void gdtSetGate(int32_t num, uint32_t base, uint32_t limit, uint8_t access, uint
  210.  
  211. gdtEntries[num].granularity |= gran & 0xF0;
  212. gdtEntries[num].access = access;
  213. -}
  214. -
  215. -
  216. -// gdtInit() - Initializes GDT and sets up all the pointers
  217. -void gdtInit() {
  218. - // Setup the gdtPtr to point to our gdtEntires
  219. - gdtPtr.limit = 0xFFFFF;
  220. - gdtPtr.base = (uint32_t)&gdtEntries;
  221. -
  222. - // Now setup the GDT entries
  223. - gdtSetGate(0, 0, 0, 0, 0); // Null segment
  224. - gdtSetGate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); // Code segment
  225. - gdtSetGate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); // Data segment
  226. +}
  227. +
  228. +
  229. +// gdtInit() - Initializes GDT and sets up all the pointers
  230. +void gdtInit() {
  231. + // Setup the gdtPtr to point to our gdtEntires
  232. + gdtPtr.limit = sizeof(gdtEntries)-1;
  233. + gdtPtr.base = (uint32_t)&gdtEntries;
  234. +
  235. + // Now setup the GDT entries
  236. + gdtSetGate(0, 0, 0, 0, 0); // Null segment
  237. + gdtSetGate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); // Code segment
  238. + gdtSetGate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); // Data segment
  239. gdtSetGate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF); // User mode code segment
  240. gdtSetGate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF); // User mode data segment.
  241. tssWrite(5, 0x10, 0x0); // Task state segment
  242. diff --git a/source/kernel/idt.c b/source/kernel/idt.c
  243. index cd08c69..8ba087b 100644
  244. --- a/source/kernel/idt.c
  245. +++ b/source/kernel/idt.c
  246. @@ -31,23 +31,23 @@ int idtInstallIR(uint8_t i, uint8_t flags, uint16_t segmentSelector, uint32_t ba
  247. idtEntries[i].flags = flags;
  248.  
  249. return 0;
  250. -}
  251. -
  252. -
  253. -
  254. -void idtInit() {
  255. - // Setup the IDT pointer.
  256. - idtPtr.limit = sizeof(idtEntry_t) * 256 - 1;
  257. - idtPtr.base_addr = (uint32_t)idtEntries;
  258. -
  259. - // Clear IDT entries table.
  260. - memset(&idtEntries, 0, sizeof(idtEntry_t)*256);
  261. -
  262. - // Enable PIC manually, pic.c DOES NOT WORK.
  263. - outportb(0x20, 0x11);
  264. - outportb(0xA0, 0x11);
  265. - outportb(0x21, 0x20);
  266. - outportb(0xA1, 0x28);
  267. +}
  268. +
  269. +
  270. +
  271. +void idtInit() {
  272. + // Setup the IDT pointer.
  273. + idtPtr.limit = sizeof(idtEntries)-1;
  274. + idtPtr.base_addr = (uint32_t)idtEntries;
  275. +
  276. + // Clear IDT entries table.
  277. + memset(&idtEntries, 0, sizeof(idtEntries));
  278. +
  279. + // Enable PIC manually, pic.c DOES NOT WORK.
  280. + outportb(0x20, 0x11);
  281. + outportb(0xA0, 0x11);
  282. + outportb(0x21, 0x20);
  283. + outportb(0xA1, 0x28);
  284. outportb(0x21, 0x04);
  285. outportb(0xA1, 0x02);
  286. outportb(0x21, 0x01);
  287. @@ -56,10 +56,10 @@ void idtInit() {
  288. outportb(0xA1, 0x0);
  289.  
  290.  
  291. -
  292. - isrInstall(); // Install handlers
  293. -
  294. - install_idt((uint32_t)&idtPtr);
  295. -
  296. - return;
  297. -}
  298. \ No newline at end of file
  299. +
  300. + isrInstall(); // Install handlers
  301. +
  302. + install_idt((uint32_t)&idtPtr);
  303. +
  304. + return;
  305. +}
  306. diff --git a/source/kernel/libc/string.c b/source/kernel/libc/string.c
  307. index 4aeff5b..b9d759b 100644
  308. --- a/source/kernel/libc/string.c
  309. +++ b/source/kernel/libc/string.c
  310. @@ -56,19 +56,19 @@ void* memmove(void* destination, const void* source, size_t n) {
  311. return destination;
  312. }
  313.  
  314. -
  315. -// memset() - Set a buffer in memory to a given value.
  316. -// Three parameters - buffer, value, amount of times to set
  317. -
  318. -void* memset(void *buf, char c, size_t n) {
  319. - unsigned char *temp = (unsigned char *)buf;
  320. - for (; n != 0; n--) temp[n] = c;
  321. - return buf;
  322. -}
  323. -
  324. -// strlen() - Returns the length of a string(size_t)
  325. -// One parameter - str
  326. -int strlen(char *str) {
  327. +
  328. +// memset() - Set a buffer in memory to a given value.
  329. +// Three parameters - buffer, value, amount of times to set
  330. +
  331. +void* memset(void *buf, char c, size_t n) {
  332. + unsigned char *temp = (unsigned char *)buf;
  333. + for (; n != 0; n--) temp[n-1] = c;
  334. + return buf;
  335. +}
  336. +
  337. +// strlen() - Returns the length of a string(size_t)
  338. +// One parameter - str
  339. +int strlen(char *str) {
  340. int i = 0;
  341. while (*str++) {
  342. i++;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement