Refactoring assembly entry point
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
OBJS = kernel_loader.asm.o ports.asm.o \
|
||||
gdt.asm.o idt.asm.o interrupts.asm.o
|
||||
|
||||
ASM = nasm
|
||||
ASMFLAGS = -f elf32
|
||||
all: $(OBJS)
|
||||
%.asm.o: %.asm
|
||||
$(ASM) $(ASMFLAGS) $< -o $@
|
||||
OBJS = main.asm.o ports.asm.o \
|
||||
gdt.asm.o idt.asm.o \
|
||||
interrupts.asm.o header.asm.o
|
||||
|
||||
ASM = nasm
|
||||
ASMFLAGS = -f elf32
|
||||
all: $(OBJS)
|
||||
%.asm.o: %.asm
|
||||
$(ASM) $(ASMFLAGS) $< -o $@
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
; vulcanOS Kernel ;
|
||||
; Developed by Marco 'icebit' Cetica ;
|
||||
; (c) 2019-2021 ;
|
||||
; Released under GPLv3 ;
|
||||
; https://github.com/ice-bit/iceOS ;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
global gdt_flush ; for drivers/gdt.c
|
||||
|
||||
section .text
|
||||
gdt_flush:
|
||||
mov eax, [esp+4] ; get address of gdt_ptr_t
|
||||
lgdt [eax] ; Load GDT
|
||||
mov ax, 0x10 ; offset in the GDT of the data segment
|
||||
mov ds, ax ; Load data segment selectors
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
mov ss, ax
|
||||
jmp 0x08:.flush ; offset in the GDT of the code segment
|
||||
.flush:
|
||||
ret
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
; vulcanOS Kernel ;
|
||||
; Developed by Marco 'icebit' Cetica ;
|
||||
; (c) 2019-2021 ;
|
||||
; Released under GPLv3 ;
|
||||
; https://github.com/ice-bit/vulcanos ;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
global gdt_flush ; for drivers/gdt.c
|
||||
|
||||
section .text
|
||||
gdt_flush:
|
||||
mov eax, [esp+4] ; get address of gdt_ptr_t
|
||||
lgdt [eax] ; Load GDT
|
||||
mov ax, 0x10 ; offset in the GDT of the data segment
|
||||
mov ds, ax ; Load data segment selectors
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
mov ss, ax
|
||||
jmp 0x08:.flush ; offset in the GDT of the code segment
|
||||
.flush:
|
||||
ret
|
||||
|
||||
19
kernel/cpu/header.asm
Normal file
19
kernel/cpu/header.asm
Normal file
@@ -0,0 +1,19 @@
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
; vulcanOS Kernel ;
|
||||
; Developed by Marco 'icebit' Cetica ;
|
||||
; (c) 2019-2021 ;
|
||||
; Released under GPLv3 ;
|
||||
; https://github.com/ice-bit/vulcanos ;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
section .multiboot_header
|
||||
header_s:
|
||||
dd 0xE85250D6 ; Magic number for multiboot
|
||||
dd 0 ; Protected mode flag
|
||||
dd header_e - header_s ; Header length
|
||||
dd 0x100000000 - (0xE85250D6 + 0 + (header_e - header_s)) ; Checksum
|
||||
|
||||
; Other flags
|
||||
dw 0 ; Type
|
||||
dw 0 ; Flags
|
||||
dw 0 ; Size
|
||||
header_e:
|
||||
@@ -1,14 +1,14 @@
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
; vulcanOS Kernel ;
|
||||
; Developed by Marco 'icebit' Cetica ;
|
||||
; (c) 2019-2021 ;
|
||||
; Released under GPLv3 ;
|
||||
; https://github.com/ice-bit/iceOS ;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
global idt_flush ; for drivers/idt.c
|
||||
|
||||
section .text
|
||||
idt_flush:
|
||||
mov eax, [esp+4] ; Retrieve idt_ptr_t*
|
||||
lidt [eax]
|
||||
ret
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
; vulcanOS Kernel ;
|
||||
; Developed by Marco 'icebit' Cetica ;
|
||||
; (c) 2019-2021 ;
|
||||
; Released under GPLv3 ;
|
||||
; https://github.com/ice-bit/vulcanos ;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
global idt_flush ; for drivers/idt.c
|
||||
|
||||
section .text
|
||||
idt_flush:
|
||||
mov eax, [esp+4] ; Retrieve idt_ptr_t*
|
||||
lidt [eax]
|
||||
ret
|
||||
|
||||
@@ -1,159 +1,159 @@
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
; vulcanOS Kernel ;
|
||||
; Developed by Marco 'icebit' Cetica ;
|
||||
; (c) 2019-2021 ;
|
||||
; Released under GPLv3 ;
|
||||
; https://github.com/ice-bit/iceOS ;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
extern isr_handler ; Defined in drivers/isr.h
|
||||
extern irq_handler ; Defined in drivers/isr.h
|
||||
|
||||
; Let's implement all ISR in a very handy way
|
||||
%macro ISR_NOERRCODE 1
|
||||
global isr%1
|
||||
isr%1:
|
||||
cli ; Disable interrupts
|
||||
push byte 0 ; Push dummy error code
|
||||
push byte %1 ; Push interrupt number
|
||||
jmp isr_common ; goto ISR handler
|
||||
%endmacro
|
||||
|
||||
%macro ISR_ERRCODE 1
|
||||
global isr%1
|
||||
isr%1:
|
||||
cli ; Disable interrupts
|
||||
push byte %1 ; Push interrupt number
|
||||
jmp isr_common ; goto ISR handler
|
||||
%endmacro
|
||||
|
||||
; Now we have to do the same thing for Interrupt Requests,
|
||||
; in this case the first parameter is the IRQ number while
|
||||
; the second one is the ISR number to be remapped to
|
||||
%macro IRQ 2
|
||||
global irq%1
|
||||
irq%1:
|
||||
cli ; Disable interrupts
|
||||
push byte 0 ; Push dummy error code
|
||||
push byte %2 ; Push interrupt number
|
||||
jmp irq_common ; goto IRQ handler
|
||||
%endmacro
|
||||
|
||||
; isr_common is a common handler for all
|
||||
; Interrupt Service Routines declared in the system
|
||||
; It's main scope is to save current register's states
|
||||
; into the stack, call the C high level handler
|
||||
; and restore the register's original values from
|
||||
; the stack
|
||||
isr_common:
|
||||
;; Save register's content into the stack ;;
|
||||
pusha ; Push edi,esi,ebp,esp,ebx,edx,ecx,eax
|
||||
|
||||
mov ax, ds ; Get 16 bits of eax(e.g ds)
|
||||
push eax
|
||||
|
||||
mov ax, 0x10 ; Load the kernel data segment descriptor
|
||||
mov ds, ax
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
|
||||
;; Call C handler ;;
|
||||
call isr_handler ; Call C handler
|
||||
|
||||
;; Restore register's content from the stack ;;
|
||||
pop eax ; Restore original data segment selector
|
||||
|
||||
mov ds, ax
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
|
||||
popa ; Pop edi,esi,ebp,esp,ebx,edx,ecx,eax
|
||||
add esp, 8 ; Cleans up pushed error code and ISR number
|
||||
sti ; Re-enable interrupts
|
||||
iret ; Pops 5 things: CS, EIP, EFLAGS, SS and ESp
|
||||
|
||||
|
||||
; irq_common is a common handler for all
|
||||
; Interrupt Requests, it's very similar to the
|
||||
; ISR one
|
||||
irq_common:
|
||||
;; Save register's content into the stack ;;
|
||||
pusha ; Push edi,esi,ebp,esp,ebx,edx,ecx,eax
|
||||
|
||||
mov ax, ds ; Get 16 bits of eax(e.g ds)
|
||||
push eax
|
||||
|
||||
mov ax, 0x10 ; Load the kernel data segment descriptor
|
||||
mov ds, ax
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
|
||||
;; Call C handler ;;
|
||||
call irq_handler ; Call C handler
|
||||
|
||||
;; Restore register's content from the stack ;;
|
||||
pop ebx ; Restore original data segment selector
|
||||
|
||||
mov ds, bx
|
||||
mov es, bx
|
||||
mov fs, bx
|
||||
mov gs, bx
|
||||
|
||||
popa ; Pop edi,esi,ebp,esp,ebx,edx,ecx,eax
|
||||
add esp, 8 ; Cleans up pushed error code and ISR number
|
||||
sti ; Re-enable interrupts
|
||||
iret ; Pops 5 things: CS, EIP, EFLAGS, SS and ESp
|
||||
|
||||
; Standard x86 ISRs (only 8,10-14 and 17 requires to push error codes to the stack)
|
||||
ISR_NOERRCODE 0
|
||||
ISR_NOERRCODE 1
|
||||
ISR_NOERRCODE 2
|
||||
ISR_NOERRCODE 3
|
||||
ISR_NOERRCODE 4
|
||||
ISR_NOERRCODE 5
|
||||
ISR_NOERRCODE 6
|
||||
ISR_NOERRCODE 7
|
||||
ISR_ERRCODE 8
|
||||
ISR_NOERRCODE 9
|
||||
ISR_ERRCODE 10
|
||||
ISR_ERRCODE 11
|
||||
ISR_ERRCODE 12
|
||||
ISR_ERRCODE 13
|
||||
ISR_ERRCODE 14
|
||||
ISR_NOERRCODE 15
|
||||
ISR_NOERRCODE 16
|
||||
ISR_NOERRCODE 17
|
||||
ISR_NOERRCODE 18
|
||||
ISR_NOERRCODE 19
|
||||
ISR_NOERRCODE 20
|
||||
ISR_NOERRCODE 21
|
||||
ISR_NOERRCODE 22
|
||||
ISR_NOERRCODE 23
|
||||
ISR_NOERRCODE 24
|
||||
ISR_NOERRCODE 25
|
||||
ISR_NOERRCODE 26
|
||||
ISR_NOERRCODE 27
|
||||
ISR_NOERRCODE 28
|
||||
ISR_NOERRCODE 29
|
||||
ISR_NOERRCODE 30
|
||||
ISR_NOERRCODE 31
|
||||
|
||||
IRQ 0, 32
|
||||
IRQ 1, 33
|
||||
IRQ 2, 34
|
||||
IRQ 3, 35
|
||||
IRQ 4, 36
|
||||
IRQ 5, 37
|
||||
IRQ 6, 38
|
||||
IRQ 7, 39
|
||||
IRQ 8, 40
|
||||
IRQ 9, 41
|
||||
IRQ 10, 42
|
||||
IRQ 11, 43
|
||||
IRQ 12, 44
|
||||
IRQ 13, 45
|
||||
IRQ 14, 46
|
||||
IRQ 15, 47
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
; vulcanOS Kernel ;
|
||||
; Developed by Marco 'icebit' Cetica ;
|
||||
; (c) 2019-2021 ;
|
||||
; Released under GPLv3 ;
|
||||
; https://github.com/ice-bit/vulcanos ;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
extern isr_handler ; Defined in drivers/isr.h
|
||||
extern irq_handler ; Defined in drivers/isr.h
|
||||
|
||||
; Let's implement all ISR in a very handy way
|
||||
%macro ISR_NOERRCODE 1
|
||||
global isr%1
|
||||
isr%1:
|
||||
cli ; Disable interrupts
|
||||
push byte 0 ; Push dummy error code
|
||||
push byte %1 ; Push interrupt number
|
||||
jmp isr_common ; goto ISR handler
|
||||
%endmacro
|
||||
|
||||
%macro ISR_ERRCODE 1
|
||||
global isr%1
|
||||
isr%1:
|
||||
cli ; Disable interrupts
|
||||
push byte %1 ; Push interrupt number
|
||||
jmp isr_common ; goto ISR handler
|
||||
%endmacro
|
||||
|
||||
; Now we have to do the same thing for Interrupt Requests,
|
||||
; in this case the first parameter is the IRQ number while
|
||||
; the second one is the ISR number to be remapped to
|
||||
%macro IRQ 2
|
||||
global irq%1
|
||||
irq%1:
|
||||
cli ; Disable interrupts
|
||||
push byte 0 ; Push dummy error code
|
||||
push byte %2 ; Push interrupt number
|
||||
jmp irq_common ; goto IRQ handler
|
||||
%endmacro
|
||||
|
||||
; isr_common is a common handler for all
|
||||
; Interrupt Service Routines declared in the system
|
||||
; It's main scope is to save current register's states
|
||||
; into the stack, call the C high level handler
|
||||
; and restore the register's original values from
|
||||
; the stack
|
||||
isr_common:
|
||||
;; Save register's content into the stack ;;
|
||||
pusha ; Push edi,esi,ebp,esp,ebx,edx,ecx,eax
|
||||
|
||||
mov ax, ds ; Get 16 bits of eax(e.g ds)
|
||||
push eax
|
||||
|
||||
mov ax, 0x10 ; Load the kernel data segment descriptor
|
||||
mov ds, ax
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
|
||||
;; Call C handler ;;
|
||||
call isr_handler ; Call C handler
|
||||
|
||||
;; Restore register's content from the stack ;;
|
||||
pop eax ; Restore original data segment selector
|
||||
|
||||
mov ds, ax
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
|
||||
popa ; Pop edi,esi,ebp,esp,ebx,edx,ecx,eax
|
||||
add esp, 8 ; Cleans up pushed error code and ISR number
|
||||
sti ; Re-enable interrupts
|
||||
iret ; Pops 5 things: CS, EIP, EFLAGS, SS and ESp
|
||||
|
||||
|
||||
; irq_common is a common handler for all
|
||||
; Interrupt Requests, it's very similar to the
|
||||
; ISR one
|
||||
irq_common:
|
||||
;; Save register's content into the stack ;;
|
||||
pusha ; Push edi,esi,ebp,esp,ebx,edx,ecx,eax
|
||||
|
||||
mov ax, ds ; Get 16 bits of eax(e.g ds)
|
||||
push eax
|
||||
|
||||
mov ax, 0x10 ; Load the kernel data segment descriptor
|
||||
mov ds, ax
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
|
||||
;; Call C handler ;;
|
||||
call irq_handler ; Call C handler
|
||||
|
||||
;; Restore register's content from the stack ;;
|
||||
pop ebx ; Restore original data segment selector
|
||||
|
||||
mov ds, bx
|
||||
mov es, bx
|
||||
mov fs, bx
|
||||
mov gs, bx
|
||||
|
||||
popa ; Pop edi,esi,ebp,esp,ebx,edx,ecx,eax
|
||||
add esp, 8 ; Cleans up pushed error code and ISR number
|
||||
sti ; Re-enable interrupts
|
||||
iret ; Pops 5 things: CS, EIP, EFLAGS, SS and ESp
|
||||
|
||||
; Standard x86 ISRs (only 8,10-14 and 17 requires to push error codes to the stack)
|
||||
ISR_NOERRCODE 0
|
||||
ISR_NOERRCODE 1
|
||||
ISR_NOERRCODE 2
|
||||
ISR_NOERRCODE 3
|
||||
ISR_NOERRCODE 4
|
||||
ISR_NOERRCODE 5
|
||||
ISR_NOERRCODE 6
|
||||
ISR_NOERRCODE 7
|
||||
ISR_ERRCODE 8
|
||||
ISR_NOERRCODE 9
|
||||
ISR_ERRCODE 10
|
||||
ISR_ERRCODE 11
|
||||
ISR_ERRCODE 12
|
||||
ISR_ERRCODE 13
|
||||
ISR_ERRCODE 14
|
||||
ISR_NOERRCODE 15
|
||||
ISR_NOERRCODE 16
|
||||
ISR_NOERRCODE 17
|
||||
ISR_NOERRCODE 18
|
||||
ISR_NOERRCODE 19
|
||||
ISR_NOERRCODE 20
|
||||
ISR_NOERRCODE 21
|
||||
ISR_NOERRCODE 22
|
||||
ISR_NOERRCODE 23
|
||||
ISR_NOERRCODE 24
|
||||
ISR_NOERRCODE 25
|
||||
ISR_NOERRCODE 26
|
||||
ISR_NOERRCODE 27
|
||||
ISR_NOERRCODE 28
|
||||
ISR_NOERRCODE 29
|
||||
ISR_NOERRCODE 30
|
||||
ISR_NOERRCODE 31
|
||||
|
||||
IRQ 0, 32
|
||||
IRQ 1, 33
|
||||
IRQ 2, 34
|
||||
IRQ 3, 35
|
||||
IRQ 4, 36
|
||||
IRQ 5, 37
|
||||
IRQ 6, 38
|
||||
IRQ 7, 39
|
||||
IRQ 8, 40
|
||||
IRQ 9, 41
|
||||
IRQ 10, 42
|
||||
IRQ 11, 43
|
||||
IRQ 12, 44
|
||||
IRQ 13, 45
|
||||
IRQ 14, 46
|
||||
IRQ 15, 47
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
; vulcanOS Kernel ;
|
||||
; Developed by Marco 'icebit' Cetica ;
|
||||
; (c) 2019-2021 ;
|
||||
; Released under GPLv3 ;
|
||||
; https://github.com/ice-bit/iceOS ;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
[BITS 32] ; We should be in protected mode
|
||||
section .multiboot
|
||||
|
||||
head_s:
|
||||
dd 0xE85250D6 ; Multiboot header magic number
|
||||
dd 0 ; Protected mode flag
|
||||
dd head_e - head_s ; Header length
|
||||
dd 0x100000000 - (0xE85250D6 + 0 + (head_e - head_s)) ; Checksum of above
|
||||
|
||||
; Other flags
|
||||
dw 0 ; type
|
||||
dw 0 ; flags
|
||||
dd 0 ; size
|
||||
|
||||
head_e:
|
||||
|
||||
GLOBAL kernel_loader
|
||||
EXTERN kernel_main
|
||||
|
||||
section .text
|
||||
kernel_loader:
|
||||
mov esp, kernel_stack + KERNEL_STACK_SZ ; Define stack pointer
|
||||
push eax ; Set multiboot header
|
||||
call kernel_main ; Jump into kernel's main function
|
||||
.loop:
|
||||
jmp .loop ; If the kernel returns, go into an infinite loop.
|
||||
; This will prevent the CPU to run non-kernel instructions
|
||||
; from the memory
|
||||
|
||||
KERNEL_STACK_SZ equ 4096 ; Stack size(4KiB)
|
||||
|
||||
section .bss
|
||||
align 4
|
||||
kernel_stack:
|
||||
resb KERNEL_STACK_SZ ; Reserve 4 KiB
|
||||
27
kernel/cpu/main.asm
Normal file
27
kernel/cpu/main.asm
Normal file
@@ -0,0 +1,27 @@
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
; vulcanOS Kernel ;
|
||||
; Developed by Marco 'icebit' Cetica ;
|
||||
; (c) 2019-2021 ;
|
||||
; Released under GPLv3 ;
|
||||
; https://github.com/ice-bit/vulcanos ;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
GLOBAL kernel_loader
|
||||
EXTERN kernel_main
|
||||
[BITS 32] ; Ensure we are in protected mode
|
||||
|
||||
section .text
|
||||
kernel_loader:
|
||||
mov esp, kernel_stack + KERNEL_STACK_SZ ; Define stack pointer
|
||||
push eax ; Set multiboot header register
|
||||
call kernel_main ; Call kernel's main function
|
||||
.loop:
|
||||
jmp .loop ; If the kernel returns, go into an endless loop
|
||||
; This will prevent the CPU to execure any non-kernel
|
||||
; instructions.
|
||||
|
||||
KERNEL_STACK_SZ equ 4096 ; Stack size(4KiB)
|
||||
|
||||
section .bss
|
||||
align 4
|
||||
kernel_stack:
|
||||
resb KERNEL_STACK_SZ ; Reserver 4KiB for kernel's stack
|
||||
@@ -1,21 +1,21 @@
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
; vulcanOS Kernel ;
|
||||
; Developed by Marco 'icebit' Cetica ;
|
||||
; (c) 2019-2021 ;
|
||||
; Released under GPLv3 ;
|
||||
; https://github.com/ice-bit/iceOS ;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
global outb ; Output from port
|
||||
global inb ; Input to port
|
||||
|
||||
outb:
|
||||
mov al, [esp + 8]
|
||||
mov dx, [esp + 4]
|
||||
out dx, al
|
||||
ret
|
||||
|
||||
inb:
|
||||
mov dx, [esp + 4]
|
||||
in al, dx
|
||||
ret
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
; vulcanOS Kernel ;
|
||||
; Developed by Marco 'icebit' Cetica ;
|
||||
; (c) 2019-2021 ;
|
||||
; Released under GPLv3 ;
|
||||
; https://github.com/ice-bit/vulcanos ;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
global outb ; Output from port
|
||||
global inb ; Input to port
|
||||
|
||||
outb:
|
||||
mov al, [esp + 8]
|
||||
mov dx, [esp + 4]
|
||||
out dx, al
|
||||
ret
|
||||
|
||||
inb:
|
||||
mov dx, [esp + 4]
|
||||
in al, dx
|
||||
ret
|
||||
|
||||
Reference in New Issue
Block a user