Espresso 0.0.1e
This commit is contained in:
249
arch/x86/boot/boot.s
Normal file
249
arch/x86/boot/boot.s
Normal file
@ -0,0 +1,249 @@
|
||||
/* Declare constants for the multiboot header. */
|
||||
.set ALIGN, 1<<0 /* align loaded modules on page boundaries */
|
||||
.set MEMINFO, 1<<1 /* provide memory map */
|
||||
.set FLAGS, ALIGN | MEMINFO /* this is the Multiboot 'flag' field */
|
||||
.set MAGIC, 0x1BADB002 /* 'magic number' lets bootloader find the header */
|
||||
.set CHECKSUM, -(MAGIC + FLAGS) /* checksum of above, to prove we are multiboot */
|
||||
|
||||
/*
|
||||
Declare a multiboot header that marks the program as a kernel. These are magic
|
||||
values that are documented in the multiboot standard. The bootloader will
|
||||
search for this signature in the first 8 KiB of the kernel file, aligned at a
|
||||
32-bit boundary. The signature is in its own section so the header can be
|
||||
forced to be within the first 8 KiB of the kernel file.
|
||||
*/
|
||||
.section .multiboot
|
||||
.align 4
|
||||
.long MAGIC
|
||||
.long FLAGS
|
||||
.long CHECKSUM
|
||||
|
||||
/*
|
||||
The multiboot standard does not define the value of the stack pointer register
|
||||
(esp) and it is up to the kernel to provide a stack. This allocates room for a
|
||||
small stack by creating a symbol at the bottom of it, then allocating 16384
|
||||
bytes for it, and finally creating a symbol at the top. The stack grows
|
||||
downwards on x86. The stack is in its own section so it can be marked nobits,
|
||||
which means the kernel file is smaller because it does not contain an
|
||||
uninitialized stack. The stack on x86 must be 16-byte aligned according to the
|
||||
System V ABI standard and de-facto extensions. The compiler will assume the
|
||||
stack is properly aligned and failure to align the stack will result in
|
||||
undefined behavior.
|
||||
*/
|
||||
.section .bss
|
||||
.align 16
|
||||
stack_bottom:
|
||||
.skip 16384 # 16 KiB
|
||||
stack_top:
|
||||
|
||||
/*
|
||||
The linker script specifies _start as the entry point to the kernel and the
|
||||
bootloader will jump to this position once the kernel has been loaded. It
|
||||
doesn't make sense to return from this function as the bootloader is gone.
|
||||
*/
|
||||
.section .text
|
||||
|
||||
.global _start
|
||||
.global _kernel_early
|
||||
/*.global loadPageDirectory
|
||||
.global enablePaging*/
|
||||
|
||||
.type _start, @function
|
||||
|
||||
/*load_page_directory:
|
||||
push %ebp
|
||||
mov %esp, %ebp
|
||||
mov 8(%esp), %eax
|
||||
mov %eax, %cr3
|
||||
mov %ebp, %esp
|
||||
pop %ebp
|
||||
ret
|
||||
|
||||
enable_paging:
|
||||
push %ebp
|
||||
mov %esp, %ebp
|
||||
mov %cr0, %eax
|
||||
or $0x80000000, %eax
|
||||
mov %eax, %cr0
|
||||
mov %ebp, %esp
|
||||
pop %ebp
|
||||
ret*/
|
||||
|
||||
enable_sse_asm:
|
||||
push %eax
|
||||
push %ebx
|
||||
push %ecx
|
||||
push %edx
|
||||
push %esi
|
||||
push %edi
|
||||
|
||||
# Check CPUID support
|
||||
pushf
|
||||
pop %eax
|
||||
mov %eax, %ecx
|
||||
xor $0x200000, %eax
|
||||
push %eax
|
||||
popf
|
||||
pushf
|
||||
pop %eax
|
||||
xor %ecx, %eax
|
||||
jz .no_cpuid
|
||||
|
||||
# CPUID function 1
|
||||
mov $1, %eax
|
||||
cpuid
|
||||
mov %edx, %ebx # EDX = SSE1/SSE2 bits
|
||||
mov %ecx, %esi # ECX = SSE3/SSSE3/SSE4.1 bits
|
||||
|
||||
test $0x02000000, %ebx # SSE
|
||||
jz .no_sse
|
||||
|
||||
# Enable SSE (required for SSE1/2/3/SSSE3/4.1)
|
||||
mov %cr0, %eax
|
||||
and $~0x4, %eax # Clear EM (bit 2)
|
||||
or $0x2, %eax # Set MP (bit 1)
|
||||
mov %eax, %cr0
|
||||
|
||||
mov %cr4, %eax
|
||||
or $0x600, %eax # Set OSFXSR | OSXMMEXCPT
|
||||
mov %eax, %cr4
|
||||
|
||||
# Set version = 1 (SSE1)
|
||||
mov $1, %eax
|
||||
|
||||
test $0x04000000, %ebx # SSE2 (bit 26)
|
||||
jz .check_sse3
|
||||
mov $2, %eax
|
||||
|
||||
.check_sse3:
|
||||
test $0x00000001, %esi # SSE3 (bit 0)
|
||||
jz .check_ssse3
|
||||
mov $3, %eax
|
||||
|
||||
.check_ssse3:
|
||||
test $0x00000200, %esi # SSSE3 (bit 9)
|
||||
jz .check_sse41
|
||||
mov $4, %eax
|
||||
|
||||
.check_sse41:
|
||||
test $0x00080000, %esi # SSE4.1 (bit 19)
|
||||
jz .set_result
|
||||
mov $5, %eax
|
||||
|
||||
.set_result:
|
||||
lea sse_initialized, %edi
|
||||
mov %eax, (%edi)
|
||||
jmp .done
|
||||
|
||||
.no_sse:
|
||||
.no_cpuid:
|
||||
lea sse_initialized, %edi
|
||||
mov $0, (%edi)
|
||||
|
||||
.done:
|
||||
pop %edi
|
||||
pop %esi
|
||||
pop %edx
|
||||
pop %ecx
|
||||
pop %ebx
|
||||
pop %eax
|
||||
ret
|
||||
|
||||
|
||||
_kernel_early:
|
||||
call _init
|
||||
|
||||
/*
|
||||
TODO: add more stuff here that needs to be ran before the main kernel code.
|
||||
*/
|
||||
|
||||
ret
|
||||
|
||||
_start:
|
||||
/*
|
||||
The bootloader has loaded us into 32-bit protected mode on a x86
|
||||
machine. Interrupts are disabled. Paging is disabled. The processor
|
||||
state is as defined in the multiboot standard. The kernel has full
|
||||
control of the CPU. The kernel can only make use of hardware features
|
||||
and any code it provides as part of itself. There's no printf
|
||||
function, unless the kernel provides its own <stdio.h> header and a
|
||||
printf implementation. There are no security restrictions, no
|
||||
safeguards, no debugging mechanisms, only what the kernel provides
|
||||
itself. It has absolute and complete power over the
|
||||
machine.
|
||||
*/
|
||||
|
||||
/*
|
||||
To set up a stack, we set the esp register to point to the top of the
|
||||
stack (as it grows downwards on x86 systems). This is necessarily done
|
||||
in assembly as languages such as C cannot function without a stack.
|
||||
*/
|
||||
movl $stack_top, %esp
|
||||
andl $0xFFFFFFF0, %esp
|
||||
movl %esp, %ebp
|
||||
|
||||
|
||||
/*
|
||||
This is a good place to initialize crucial processor state before the
|
||||
high-level kernel is entered. It's best to minimize the early
|
||||
environment where crucial features are offline. Note that the
|
||||
processor is not fully initialized yet: Features such as floating
|
||||
point instructions and instruction set extensions are not initialized
|
||||
yet. The GDT should be loaded here. Paging should be enabled here.
|
||||
C++ features such as global constructors and exceptions will require
|
||||
runtime support to work as well.
|
||||
*/
|
||||
|
||||
cli /* Just in case */
|
||||
|
||||
call enable_sse_asm
|
||||
|
||||
push %eax
|
||||
push %ebx
|
||||
|
||||
|
||||
/*
|
||||
Call _kernel_early, early low-level initialization will happen there;
|
||||
please note that while _kernel_early is written in assembler,
|
||||
kernel_early is written in C. (kernel_early is called by _kernel_early, don't be confused. ;) )
|
||||
*/
|
||||
call _kernel_early
|
||||
|
||||
/*
|
||||
Enter the high-level kernel. The ABI requires the stack is 16-byte
|
||||
aligned at the time of the call instruction (which afterwards pushes
|
||||
the return pointer of size 4 bytes). The stack was originally 16-byte
|
||||
aligned above and we've pushed a multiple of 16 bytes to the
|
||||
stack since (pushed 0 bytes so far), so the alignment has thus been
|
||||
preserved and the call is well defined.
|
||||
*/
|
||||
call kernel_main
|
||||
|
||||
/*
|
||||
If the system has nothing more to do, put the computer into an
|
||||
infinite loop. To do that:
|
||||
1) Disable interrupts with cli (clear interrupt enable in eflags).
|
||||
They are already disabled by the bootloader, so this is not needed.
|
||||
Mind that you might later enable interrupts and return from
|
||||
kernel_main (which is sort of nonsensical to do).
|
||||
2) Wait for the next interrupt to arrive with hlt (halt instruction).
|
||||
Since they are disabled, this will lock up the computer.
|
||||
3) Jump to the hlt instruction if it ever wakes up due to a
|
||||
non-maskable interrupt occurring or due to system management mode.
|
||||
*/
|
||||
cli
|
||||
1: hlt
|
||||
jmp 1b
|
||||
|
||||
/*
|
||||
Set the size of the _start symbol to the current location '.' minus its start.
|
||||
This is useful when debugging or when you implement call tracing.
|
||||
*/
|
||||
.size _start, . - _start
|
||||
|
||||
|
||||
.section .data
|
||||
|
||||
.global sse_initialized
|
||||
|
||||
sse_initialized: .int 0
|
14
arch/x86/gdt.asm
Normal file
14
arch/x86/gdt.asm
Normal file
@ -0,0 +1,14 @@
|
||||
global gdt_flush
|
||||
|
||||
gdt_flush:
|
||||
mov eax, [esp + 4]
|
||||
lgdt [eax]
|
||||
jmp 0x08:.flush_label ; long jump to reload CS with new segment
|
||||
.flush_label:
|
||||
mov ax, 0x10
|
||||
mov ds, ax
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
mov ss, ax
|
||||
ret
|
167
arch/x86/isr.asm
Normal file
167
arch/x86/isr.asm
Normal file
@ -0,0 +1,167 @@
|
||||
[BITS 32]
|
||||
[GLOBAL isr_stub_table]
|
||||
[GLOBAL irq_stub_table]
|
||||
extern irq_handler ; C function
|
||||
|
||||
section .text
|
||||
|
||||
; --------------------------------------------
|
||||
; ------------------- IRQs -------------------
|
||||
; --------------------------------------------
|
||||
|
||||
%macro irq_stub 1
|
||||
irq_stub_%+%1:
|
||||
pusha
|
||||
push ds
|
||||
push es
|
||||
push fs
|
||||
push gs
|
||||
|
||||
mov ax, 0x10
|
||||
mov ds, ax
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
|
||||
push dword %1 ; Push IRQ number
|
||||
call irq_handler
|
||||
add esp, 4 ; Clean up stack
|
||||
|
||||
; Send EOI
|
||||
mov al, 0x20
|
||||
mov bl, %1
|
||||
cmp bl, 8
|
||||
jb .skip_slave_eoi
|
||||
out 0xA0, al
|
||||
.skip_slave_eoi:
|
||||
out 0x20, al
|
||||
|
||||
pop gs
|
||||
pop fs
|
||||
pop es
|
||||
pop ds
|
||||
popa
|
||||
iret
|
||||
%endmacro
|
||||
|
||||
|
||||
; Define 16 IRQ stubs
|
||||
irq_stub 0
|
||||
irq_stub 1
|
||||
irq_stub 2
|
||||
irq_stub 3
|
||||
irq_stub 4
|
||||
irq_stub 5
|
||||
irq_stub 6
|
||||
irq_stub 7
|
||||
irq_stub 8
|
||||
irq_stub 9
|
||||
irq_stub 10
|
||||
irq_stub 11
|
||||
irq_stub 12
|
||||
irq_stub 13
|
||||
irq_stub 14
|
||||
irq_stub 15
|
||||
|
||||
irq_stub_table:
|
||||
%assign i 0
|
||||
%rep 16
|
||||
dd irq_stub_%+i
|
||||
%assign i i+1
|
||||
%endrep
|
||||
|
||||
; --------------------------------------------
|
||||
; ------------------- ISRs -------------------
|
||||
; --------------------------------------------
|
||||
|
||||
; Common exception handler entry point
|
||||
isr_common_handler:
|
||||
pusha ; Push general-purpose registers
|
||||
push ds
|
||||
push es
|
||||
push fs
|
||||
push gs
|
||||
|
||||
; Load known-good segment selectors
|
||||
mov ax, 0x10
|
||||
mov ds, ax
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
|
||||
; Print exception number and error code
|
||||
; You can insert direct port writes or call kernel logging function here
|
||||
; For now, just hang
|
||||
.halt:
|
||||
cli
|
||||
hlt
|
||||
jmp .halt
|
||||
|
||||
; If you want to later restore:
|
||||
pop gs
|
||||
pop fs
|
||||
pop es
|
||||
pop ds
|
||||
popa
|
||||
add esp, 8 ; clean up int_no + err_code
|
||||
iret
|
||||
|
||||
; Macro: for exceptions with error code
|
||||
%macro isr_err_stub 1
|
||||
isr_stub_%+%1:
|
||||
cli
|
||||
push dword %1 ; interrupt number
|
||||
jmp isr_common_handler
|
||||
%endmacro
|
||||
|
||||
; Macro: for exceptions without error code
|
||||
%macro isr_no_err_stub 1
|
||||
isr_stub_%+%1:
|
||||
cli
|
||||
push dword 0 ; fake error code
|
||||
push dword %1 ; interrupt number
|
||||
jmp isr_common_handler
|
||||
%endmacro
|
||||
|
||||
; Define all 32 exception stubs
|
||||
isr_no_err_stub 0
|
||||
isr_no_err_stub 1
|
||||
isr_no_err_stub 2
|
||||
isr_no_err_stub 3
|
||||
isr_no_err_stub 4
|
||||
isr_no_err_stub 5
|
||||
isr_no_err_stub 6
|
||||
isr_no_err_stub 7
|
||||
isr_err_stub 8
|
||||
isr_no_err_stub 9
|
||||
isr_err_stub 10
|
||||
isr_err_stub 11
|
||||
isr_err_stub 12
|
||||
isr_err_stub 13
|
||||
isr_err_stub 14
|
||||
isr_no_err_stub 15
|
||||
isr_no_err_stub 16
|
||||
isr_err_stub 17
|
||||
isr_no_err_stub 18
|
||||
isr_no_err_stub 19
|
||||
isr_no_err_stub 20
|
||||
isr_no_err_stub 21
|
||||
isr_no_err_stub 22
|
||||
isr_no_err_stub 23
|
||||
isr_no_err_stub 24
|
||||
isr_no_err_stub 25
|
||||
isr_no_err_stub 26
|
||||
isr_no_err_stub 27
|
||||
isr_no_err_stub 28
|
||||
isr_no_err_stub 29
|
||||
isr_err_stub 30
|
||||
isr_no_err_stub 31
|
||||
|
||||
; Create jump table
|
||||
isr_stub_table:
|
||||
%assign i 0
|
||||
%rep 32
|
||||
dd isr_stub_%+i
|
||||
%assign i i+1
|
||||
%endrep
|
||||
|
74
arch/x86/misc_asm.s
Normal file
74
arch/x86/misc_asm.s
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
A bunch of functions I could have written in C (or not), but decided not to,
|
||||
Mostly so I could get more experience in assembler.
|
||||
*/
|
||||
|
||||
/*
|
||||
DO NOT USE "pusha" AND/OR "popa"!!!
|
||||
THEY ARE DEPRECATED (or something like that)
|
||||
AND SHOULD NOT BE USED!!!
|
||||
INSTEAD PUSH/POP REGISTERS ON/OFF THE STACK INDIVIDUALLY!!!
|
||||
*/
|
||||
|
||||
|
||||
.section .text
|
||||
|
||||
.global _enable_paging_asm
|
||||
|
||||
.global _push_regs
|
||||
.global _pop_regs
|
||||
|
||||
.global _hang_asm
|
||||
.global _halt_asm
|
||||
|
||||
.global _sti_asm
|
||||
|
||||
|
||||
_enable_paging_asm:
|
||||
push %eax
|
||||
|
||||
mov %cr0, %eax
|
||||
or $0x80000000, %eax
|
||||
mov %eax, %cr0
|
||||
|
||||
pop %eax
|
||||
|
||||
ret
|
||||
|
||||
_push_regs:
|
||||
push %eax
|
||||
push %ebx
|
||||
push %ecx
|
||||
push %edx
|
||||
push %esi
|
||||
push %edi
|
||||
push %esp
|
||||
push %ebp
|
||||
|
||||
ret
|
||||
|
||||
_pop_regs:
|
||||
pop %ebp
|
||||
pop %esp
|
||||
pop %edi
|
||||
pop %esi
|
||||
pop %edx
|
||||
pop %ecx
|
||||
pop %ebx
|
||||
pop %eax
|
||||
|
||||
ret
|
||||
|
||||
_hang_asm:
|
||||
hlt
|
||||
jmp _hang_asm
|
||||
|
||||
_halt_asm:
|
||||
nop
|
||||
ret
|
||||
|
||||
_sti_asm:
|
||||
sti
|
||||
ret
|
||||
|
||||
.section .data
|
5
arch/x86/pit.asm
Normal file
5
arch/x86/pit.asm
Normal file
@ -0,0 +1,5 @@
|
||||
|
||||
; NOTE: the code in this section is mostly copied from the OSDev wiki. (wiki.osdev.org) {though osdev.org/osdev.com works just fine.}
|
||||
|
||||
|
||||
|
86
arch/x86/vga_fonts.asm
Normal file
86
arch/x86/vga_fonts.asm
Normal file
@ -0,0 +1,86 @@
|
||||
|
||||
global _get_fonts_asm
|
||||
|
||||
_get_fonts_asm:
|
||||
|
||||
; NOTE: this code is taken from https://wiki.osdev.org/VGA_Fonts
|
||||
|
||||
;in: edi=4k buffer
|
||||
;out: buffer filled with font
|
||||
;clear even/odd mode
|
||||
mov dx, 03ceh
|
||||
mov ax, 5
|
||||
out dx, ax
|
||||
;map VGA memory to 0A0000h
|
||||
mov ax, 0406h
|
||||
out dx, ax
|
||||
;set bitplane 2
|
||||
mov dx, 03c4h
|
||||
mov ax, 0402h
|
||||
out dx, ax
|
||||
;clear even/odd mode (the other way, don't ask why)
|
||||
mov ax, 0604h
|
||||
out dx, ax
|
||||
;copy charmap
|
||||
mov esi, 0A0000h
|
||||
mov ecx, 256
|
||||
;copy 16 bytes to bitmap
|
||||
@@: movsd
|
||||
movsd
|
||||
movsd
|
||||
movsd
|
||||
;skip another 16 bytes
|
||||
add esi, 16
|
||||
loop @@
|
||||
;restore VGA state to normal operation
|
||||
mov ax, 0302h
|
||||
out dx, ax
|
||||
mov ax, 0204h
|
||||
out dx, ax
|
||||
mov dx, 03ceh
|
||||
mov ax, 1005h
|
||||
out dx, ax
|
||||
mov ax, 0E06h
|
||||
out dx, ax
|
||||
|
||||
|
||||
_set_fonts_asm:
|
||||
; NOTE: this code is taken from https://wiki.osdev.org/VGA_Fonts and edited a little bit
|
||||
|
||||
;in: esi=4k buffer
|
||||
;out: buffer filled with font
|
||||
;clear even/odd mode
|
||||
mov dx, 03ceh
|
||||
mov ax, 5
|
||||
out dx, ax
|
||||
;map VGA memory to 0A0000h
|
||||
mov ax, 0406h
|
||||
out dx, ax
|
||||
;set bitplane 2
|
||||
mov dx, 03c4h
|
||||
mov ax, 0402h
|
||||
out dx, ax
|
||||
;clear even/odd mode (the other way, don't ask why)
|
||||
mov ax, 0604h
|
||||
out dx, ax
|
||||
;copy charmap
|
||||
mov edi, 0A0000h
|
||||
mov ecx, 256
|
||||
;copy 16 bytes to bitmap
|
||||
m: movsd
|
||||
movsd
|
||||
movsd
|
||||
movsd
|
||||
;skip another 16 bytes
|
||||
add esi, 16
|
||||
loop m
|
||||
;restore VGA state to normal operation
|
||||
mov ax, 0302h
|
||||
out dx, ax
|
||||
mov ax, 0204h
|
||||
out dx, ax
|
||||
mov dx, 03ceh
|
||||
mov ax, 1005h
|
||||
out dx, ax
|
||||
mov ax, 0E06h
|
||||
out dx, ax
|
Reference in New Issue
Block a user