Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 33 additions & 64 deletions kernel/asm/sector.asm → kernel/asm/bootsector.asm
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use16

org 0x7C00
ORG 0x7C00
SECTION .text
USE16

boot: ; dl comes with disk
; initialize segment registers
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax

; initialize stack
mov sp, 0x7C00

Expand All @@ -22,9 +23,9 @@ boot: ; dl comes with disk
call print_num
call print_line

mov ax, (fs_header - boot)/512
mov ax, (fs_header - boot) / 512
mov bx, fs_header
mov cx, (kernel_file.end - fs_header)/512
mov cx, (startup_end - fs_header) / 512
xor dx, dx
call load

Expand All @@ -34,6 +35,17 @@ boot: ; dl comes with disk

jmp startup

; load some sectors from disk to a buffer in memory
; buffer has to be below 1MiB
; IN
; ax: start sector
; bx: offset of buffer
; cx: number of sectors (512 Bytes each)
; dx: segment of buffer
; CLOBBER
; ax, bx, cx, dx, si
; TODO rewrite to (eventually) move larger parts at once
; if that is done increase buffer_size_sectors in startup-common to that (max 0x80000 - startup_end)
load:
cmp cx, 64
jbe .good_size
Expand All @@ -43,7 +55,7 @@ load:
call load
popa
add ax, 64
add dx, 64*512/16
add dx, 64 * 512 / 16
sub cx, 64

jmp load
Expand Down Expand Up @@ -86,59 +98,16 @@ load:
jc error
ret

print_char:
mov ah, 0x0e
int 0x10
ret

print_num:
mov cx, 4
.loop:
mov al, bh
shr al, 4
and al, 0xF

cmp al, 0xA
jb .below_a

add al, 'A' - '0' - 0xA
.below_a:
add al, '0'

push cx
push bx
call print_char
pop bx
pop cx

shl bx, 4
loop .loop

ret

print_line:
mov si, line
call print
ret

print:
.loop:
lodsb
or al, al
jz .done
call print_char
jmp .loop
.done:
ret

error:
mov si, errored
call print
call print_line
mov si, errored
call print
call print_line
.halt:
cli
hlt
jmp .halt
cli
hlt
jmp .halt

%include "asm/print16.asm"

name: db "Redox Loader",0
loading: db "Loading",0
Expand All @@ -149,13 +118,13 @@ line: db 13,10,0
disk: db 0

DAPACK:
db 0x10
db 0
.count: dw 0 ; int 13 resets this to # of blocks actually read/written
.buf: dw 0 ; memory buffer destination address (0:7c00)
.seg: dw 0 ; in memory page zero
.addr: dd 0 ; put the lba to read in this spot
dd 0 ; more storage bytes only for big lba's ( > 4 bytes )
db 0x10
db 0
.count: dw 0 ; int 13 resets this to # of blocks actually read/written
.buf: dw 0 ; memory buffer destination address (0:7c00)
.seg: dw 0 ; in memory page zero
.addr: dd 0 ; put the lba to read in this spot
dd 0 ; more storage bytes only for big lba's ( > 4 bytes )

times 510-($-$$) db 0
db 0x55
Expand Down
46 changes: 46 additions & 0 deletions kernel/asm/descriptor_flags.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
attrib:
.present equ 1 << 7
.ring1 equ 1 << 5
.ring2 equ 1 << 6
.ring3 equ 1 << 5 | 1 << 6
.user equ 1 << 4
;user
.code equ 1 << 3
; code
.conforming equ 1 << 2
.readable equ 1 << 1
; data
.expand_down equ 1 << 2
.writable equ 1 << 1
.accessed equ 1 << 0
;system
; legacy
.tssAvailabe16 equ 0x1
.ldt equ 0x2
.tssBusy16 equ 0x3
.call16 equ 0x4
.task equ 0x5
.interrupt16 equ 0x6
.trap16 equ 0x7
.tssAvailabe32 equ 0x9
.tssBusy32 equ 0xB
.call32 equ 0xC
.interrupt32 equ 0xE
.trap32 equ 0xF
; long mode
.ldt32 equ 0x2
.tssAvailabe64 equ 0x9
.tssBusy64 equ 0xB
.call64 equ 0xC
.interrupt64 equ 0xE
.trap64 equ 0xF

flags:
.granularity equ 1 << 7
.available equ 1 << 4
;user
.default_operand_size equ 1 << 6
; code
.long_mode equ 1 << 5
; data
.reserved equ 1 << 5
8 changes: 8 additions & 0 deletions kernel/asm/gdt_entry.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
struc GDTEntry
.limitl resw 1
.basel resw 1
.basem resb 1
.attribute resb 1
.flags__limith resb 1
.baseh resb 1
endstruc
139 changes: 70 additions & 69 deletions kernel/asm/initialize.asm
Original file line number Diff line number Diff line change
@@ -1,80 +1,81 @@
SECTION .text
[BITS 16]
USE16

initialize:
.fpu: ;enable fpu
mov eax, cr4
or eax, 0x200
mov cr4, eax
mov eax, 0xB7F
push eax
fldcw [esp]
pop eax
ret
mov eax, cr4
or eax, 0x200
mov cr4, eax
mov eax, 0xB7F
push eax
fldcw [esp]
pop eax
ret

.sse: ;enable sse
mov eax, cr0
and al, 11111011b
or al, 00000010b
mov cr0, eax
mov eax, cr4
or ax, 0000011000000000b
mov cr4, eax
ret
mov eax, cr0
and al, 11111011b
or al, 00000010b
mov cr0, eax
mov eax, cr4
or ax, 0000011000000000b
mov cr4, eax
ret

;PIT Frequency
;If using nanoseconds, to minimize drift, one should find a frequency as close to an integer nanosecond value in wavelength
;Divider Hz Nanoseconds Properties
;2685 444.38795779019242706393 2250286.00003631746492922946 Best For Context Switching
;5370 222.19397889509621353196 4500572.00007263492985856020
;21029 56.73981961418358774390 17624306.99991199998882825455
;23714 50.31549576902532962244 19874592.99994831745375667118
;26399 45.19798729749864262535 22124878.99998463491868476373
;29084 41.02536331545408701233 24375165.00002095238361424615
;31769 37.55804925136663623868 26625451.00005726984854313455
;34454 34.63115071302799868423 28875737.00009358731347639618
;50113 23.80982313305263437963 41999471.99993295237244784676
;52798 22.59899364874932131267 44249757.99996926983737931766
;55483 21.50535599492937776736 46500044.00000558730230583335 Lowest Drift
;58168 20.51268165772704350616 48750330.00004190476724037528
;60853 19.60760630809765610021 51000616.00007822223218031738
;Divider Hz Nanoseconds Properties
;2685 444.38795779019242706393 2250286.00003631746492922946 Best For Context Switching
;5370 222.19397889509621353196 4500572.00007263492985856020
;21029 56.73981961418358774390 17624306.99991199998882825455
;23714 50.31549576902532962244 19874592.99994831745375667118
;26399 45.19798729749864262535 22124878.99998463491868476373
;29084 41.02536331545408701233 24375165.00002095238361424615
;31769 37.55804925136663623868 26625451.00005726984854313455
;34454 34.63115071302799868423 28875737.00009358731347639618
;50113 23.80982313305263437963 41999471.99993295237244784676
;52798 22.59899364874932131267 44249757.99996926983737931766
;55483 21.50535599492937776736 46500044.00000558730230583335 Lowest Drift
;58168 20.51268165772704350616 48750330.00004190476724037528
;60853 19.60760630809765610021 51000616.00007822223218031738

.pit:
;initialize the PIT
mov ax, 2685 ;this is the divider for the PIT
out 0x40, al
rol ax, 8
out 0x40, al
;DISABLED ;enable rtc interrupt
;mov al, 0xB
;out 0x70, al
;rol ax, 8
;in al, 0x71
;rol ax, 8
;out 0x70, al
;rol ax, 8
;or al, 0x40
;out 0x71, al
ret
;initialize the PIT
mov ax, 2685 ;this is the divider for the PIT
out 0x40, al
rol ax, 8
out 0x40, al
;DISABLED ;enable rtc interrupt
;mov al, 0xB
;out 0x70, al
;rol ax, 8
;in al, 0x71
;rol ax, 8
;out 0x70, al
;rol ax, 8
;or al, 0x40
;out 0x71, al
ret

.pic: ;sets up IRQs at int 20-2F
mov al, 0x11
out 0x20, al
out 0xA0, al
mov al, 0x20 ;IRQ0 vector
out 0x21, al
mov al, 0x28 ;IRQ8 vector
out 0xA1, al
mov al, 4
out 0x21, al
mov al, 2
out 0xA1, al
mov al, 1
out 0x21, al
out 0xA1, al
xor al, al ;no IRQ masks
out 0x21, al
out 0xA1, al
mov al, 0x20 ;reset PIC's
out 0xA0, al
out 0x20, al
ret
.pic: ;sets up IRQs at int 20-2F
mov al, 0x11
out 0x20, al
out 0xA0, al
mov al, 0x20 ;IRQ0 vector
out 0x21, al
mov al, 0x28 ;IRQ8 vector
out 0xA1, al
mov al, 4
out 0x21, al
mov al, 2
out 0xA1, al
mov al, 1
out 0x21, al
out 0xA1, al
xor al, al ;no IRQ masks
out 0x21, al
out 0xA1, al
mov al, 0x20 ;reset PIC's
out 0xA0, al
out 0x20, al
ret
Loading