From c7888c9f252d14784c0f78e2c12bd994a0602fbf Mon Sep 17 00:00:00 2001 From: c0repwn3r Date: Thu, 12 May 2022 21:12:29 -0400 Subject: [PATCH] it all broke. im just going to rewrite it all --- Makefile | 2 +- bin/boot.bin | Bin 512 -> 512 bytes bin/kernel.bin | Bin 7027 -> 4097 bytes obj/kernel/kernel.o | Bin 5604 -> 5604 bytes obj/kernel/platform/interrupts/int.o | Bin 4072 -> 4532 bytes obj/kernel/platform/interrupts/int_lowlevel.o | Bin 2240 -> 3344 bytes os-image.bin | Bin 7539 -> 4609 bytes src/boot/protected/gdt.asm | 12 ++-- src/kernel/platform/interrupts/int.c | 8 ++- src/kernel/platform/interrupts/int.h | 9 ++- .../platform/interrupts/int_lowlevel.asm | 56 ++++++++++++++++-- 11 files changed, 74 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index c4564c7..825ff4d 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ kernel.elf: obj/kernel/entry.o ${CO_FILES} ${AO_FILES} i386-elf-ld -o $@ -Ttext 0x1000 $^ run: clean os-image.bin - qemu-system-x86_64 -hdd os-image.bin + qemu-system-x86_64 -hdd os-image.bin -d int debug: clean os-image.bin kernel.elf qemu-system-x86_64 -s -S -hdd os-image.bin & diff --git a/bin/boot.bin b/bin/boot.bin index 9a84534152acb62b4e0b43f21d2a5372a6d90276..1a008f37cd371778e54af15a710ac9f0c04af6dc 100644 GIT binary patch delta 13 UcmZo*X<*slz{tos*^zNS02rSGm;e9( delta 13 UcmZo*X<*slz{toq*^zNS02sIfod5s; diff --git a/bin/kernel.bin b/bin/kernel.bin index 746678e2ba3b18e71b6d36234d5b558b0dd2bd32..959dbeeaaa895bbf25e8df39b8eab95d91b146d2 100755 GIT binary patch literal 4097 zcmb7{aco;h9mmg3+`M(Ber?k&Ti5lijHwf98U+wkMPOPE8kR7#)#{{CR1-hPHjW+q z>_AY{MKY6YKO6M3da7W2J( zer}R`Lc+-}es{mm{qFbs-S6&Rj7u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%mfxib8hrM01Wv#SSvm^!1XFVzx^&5iDu_|8u;xu0O z)!W7;{spsZIQXu$26N>nN-+nTO&kAcUG@j_ijPxNrwEEI7Yy&1h=uz>s_^Tww)jp* z=iI|WAPVdxa#9N76i=-51vT7-PC2HmXeU&QD#F@BJJGw`x1+VYBJ(u<%;|#yRo*#= zkK45@T~U1m{#|>|kG_r4qGs|1z=f05E?fV48SEG1KS7kV}KA3RPY!;nu%HZQ;(vER@Il8_cH$i zbJ`fIN@spM^F7RWGuN0?0j??swYjQ3VqRx{iTN6H+EAH$D zVME-#OPJI+`jq}%BbPDK`h<})Q`w9z_V@Jm^ol17nM^7(qEioB?j1HJjC6K<%*f>7 zS`1`T`BXfe`hhX552wuWbbL}b3qwPOX(kHk^dvnV4`;_GbE%PJUVmWdLEU&bZ^Xy+ z-u=D3J^EMEX`NN3o-<4%H(?C-&=7GfnKJe9Ty`V}Tdz5h8pb5|#Lc}a^Bz5(8P+pd z-T3}E2AX;{r>Dlo(*|1U)TXigbo!=_X te;o!WvG%WxKpTNJ0&N7^2(%GsBhW^mjX)cLHUez~+6c4}_&-M={2$kUM-l)4 diff --git a/obj/kernel/kernel.o b/obj/kernel/kernel.o index 987d3fc7f64f83188831191a49b4050a4df74a9a..8ed7c05b6124c93a4667ff76dc6fdb8fd85d4572 100644 GIT binary patch delta 14 VcmaE&{X~0%4lASDW?k0p0sty~1k(Tj delta 14 VcmaE&{X~0%4lASlW?k0p0styg1k3;c diff --git a/obj/kernel/platform/interrupts/int.o b/obj/kernel/platform/interrupts/int.o index 14dbc9596b1bdbfb7d6c7b5cc76b3c0a32e90fa5..dab89eb69123e8cbbd32e34008f48d3bbf3e0ac6 100644 GIT binary patch delta 2112 zcmY*aZ-`V?6hHU9ci+63H}B2Ab^pxHwmZA)?4n}pCT1$SsUKX+NF+-4!?^CgU5BP! zo0-ibbtE$)A{{UI#U%(SNvbbO5*R2UV&sQF`V>WmXhad1AW7Koyt!fy-19qse)rsS z?>+C%t?sjZM|+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L*zUw`PORs)bNYEsL(l2xIW0Y>r{^^FoUWeJ)^qy$PG8^Y>pOjY zr?2ny^_{-H)7N+U`c7Zp>D%V?ZFBnKWf2D4QS)xDXi!T2ZU`5XOG$bE%lK+?9VvfP zWL(aRu#4P6_K~~DedIy%Fqt8bkXf=wPLflkyg#Jh1@aPkmApaTA@7rq$fx8BavIbX zO1&ay$T#FW@;&*H{6u~xzml`$H*${rN&X_|N%@tM{#9fRSxctKWn?|Mf?Pv3l1=0$ PQvOen`P<2EvWNTwb}hQ# diff --git a/os-image.bin b/os-image.bin index 5d875f0b0ef0cca3d365542c89a4964b0875f422..4683e53cb0185939e44ba4d0c2e1f5761f0fd48b 100644 GIT binary patch delta 1920 zcmZwIPfXKL90%}U%jPzjZo)wRwVOi|PD+d>qC~5)@c@U#3m6kK60O+i1)?`8WFdh~ z8caEF%EANt_3OOL7zalQDUj zn^a=*qSDW&4@+^@7%R)2Pmlc?o@rvi?&0{bXsquL`0Juk;}A+ZM1WKXXNpXDBMa{K zo1)R=5cmPnP#rdvexRx|!F!LQ$L4~k#XEvmac|o7mC-iwem2%-~TXVIx$dq&K1453%d0Z)lJcX|d zpq3l5h)TUc=AezjE0)ksVIzSWTh0zDONnIALE%rli`&=jr0ksq1JxDQ$Q8e5A$QUN zJ1tl-Wz81My@hGn`+UH{?b_n9Y-5eCb~k08EEuRB3ZH3~(o1Ep1>8f~B?|^>357>9 zFuVTzFT0dVx8;yN3cs$>ivwyIh2xu0(^L7m%c;D(DYwAA6t-K!3JOmZLM{75S5jGC zD6_DN!rv=a>#nA7ib4&asv0WqQ;ATZu)_k1lr>o}Q2k*=+y24}QaMop@B>qVe`qH# zmoWpFQOp;NvkKr4rWK=Mo?zZ%mNC`U0B12bFhiI(nC}=*4Ztx>unjj+Oae29vE$49 SAf^S=jd?00J{P~_IM08?_`tsa delta 1770 zcmeH_&1(}u7>8#kNwz+g1C42P0Hk5o=3X#YMqVa}d;e$thM((t|hKltxGs zEW(JVAlnASgI_cvr3Wb?L=d4tK|B->(!-)fLQ@n<>iV0vSpNvK@H{i`yz|b?4l~_T zJN431l$OG24jNA_+aCLVTzhSM*|xj!h?om@ z7;#*zf(0|SXQgc~dB;0=yR0duGbl>I+LSwt6Rx@^$$jLb@j!>7QqsD z14`f>sDMvk4SWSI_yM-ScG$wr;DVY!2(*GQH~@OU5pWEg07KvmI1fg_B`_8#jOvSu UsP5AnH144B2L7)%;A2et1I^(3Bme*a diff --git a/src/boot/protected/gdt.asm b/src/boot/protected/gdt.asm index 2618221..db35b41 100644 --- a/src/boot/protected/gdt.asm +++ b/src/boot/protected/gdt.asm @@ -14,12 +14,12 @@ gdt_code: ; GDT for data segment gdt_data: - dw 0xffff - dw 0x0 - db 0x0 - db 10010010b - db 11001111b - db 0x0 + dw 0xffff ; segment length + dw 0x0 ; segment base + db 0x0 ; segment base + db 10010010b ; flags + db 11001111b ; flags + segment length + db 0x0 ; segment base gdt_end: diff --git a/src/kernel/platform/interrupts/int.c b/src/kernel/platform/interrupts/int.c index 3e63985..4b77162 100644 --- a/src/kernel/platform/interrupts/int.c +++ b/src/kernel/platform/interrupts/int.c @@ -1,8 +1,14 @@ #include "int.h" #include "../../print.h" -void exception_handler() { +int count = 0; + +void exception_handler(registers_t r) { print_str("I"); + count++; + if (count > 4) { + __asm__ __volatile__("cli; hlt"); + } } void idt_set_descriptor(u8 vector, void* isr, u8 flags) { diff --git a/src/kernel/platform/interrupts/int.h b/src/kernel/platform/interrupts/int.h index a58b890..3f36fc9 100644 --- a/src/kernel/platform/interrupts/int.h +++ b/src/kernel/platform/interrupts/int.h @@ -21,8 +21,15 @@ typedef struct { static idtr_t idtr; +typedef struct { + u32 ds; /* Data segment selector */ + u32 edi, esi, ebp, esp, ebx, edx, ecx, eax; /* Pushed by pusha. */ + u32 int_no, err_code; /* Interrupt number and error code (if applicable) */ + u32 eip, cs, eflags, useresp, ss; /* Pushed by the processor automatically */ +} registers_t; + __attribute__((noreturn)) -void exception_handler(void); +void exception_handler(registers_t r); void idt_set_descriptor(u8 vector, void* isr, u8 flags); diff --git a/src/kernel/platform/interrupts/int_lowlevel.asm b/src/kernel/platform/interrupts/int_lowlevel.asm index a38fd41..0d2f397 100644 --- a/src/kernel/platform/interrupts/int_lowlevel.asm +++ b/src/kernel/platform/interrupts/int_lowlevel.asm @@ -1,16 +1,64 @@ %macro isr_err_stub 1 isr_stub_%+%1: - call exception_handler - iret + push byte %1 ; Push interrupt no to stack + + ; 1. Save CPU state + pusha ; Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax + mov ax, ds ; Lower 16-bits of eax = ds. + push eax ; save the data segment descriptor + mov ax, 0x10 ; kernel data segment descriptor + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + + ; 2. Call C handler + call exception_handler + + ; 3. Restore state + pop eax + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + popa + add esp, 8 ; Cleans up the pushed error code and pushed ISR number + sti + iret ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP %endmacro ; if writing for 64-bit, use iretq instead %macro isr_no_err_stub 1 isr_stub_%+%1: - call exception_handler - iret + push byte 0 ; No error occured + push byte %1 ; Push interrupt no to stack + + ; 1. Save CPU state + pusha ; Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax + mov ax, ds ; Lower 16-bits of eax = ds. + push eax ; save the data segment descriptor + mov ax, 0x10 ; kernel data segment descriptor + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + + ; 2. Call C handler + call exception_handler + + ; 3. Restore state + pop eax + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + popa + add esp, 8 ; Cleans up the pushed error code and pushed ISR number + sti + iret ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP %endmacro extern exception_handler + isr_no_err_stub 0 isr_no_err_stub 1 isr_no_err_stub 2