Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduler & AHCI/SATAPI #150

Merged
merged 12 commits into from
Jun 10, 2024
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ run_remote_mon:

run_ahci_sata:
$(QEMU) $(QEMU_FLAGS) -serial mon:stdio \
-device ahci,id=ahci \
-device ahci,id=ahci,debug=3 \
-trace "ahci*" \
-drive id=thatdisk,file=disk.img,if=none \
-device ide-hd,drive=thatdisk,bus=ahci.0 \
-drive id=thatcdrom,file=/dev/cdrom,if=none \
-device ide-cd,drive=thatcdrom,bus=ahci.1 \
# -trace "ahci*" \
# -drive id=thatcdrom,file=TEST.iso,if=none \
# -device ide-cd,drive=thatcdrom,bus=ahci.1 \

run_disks:
$(QEMU) $(QEMU_FLAGS) -serial mon:stdio -hda disk1.img -hdb disk2.img -hdd disk3.img
Expand Down
4 changes: 2 additions & 2 deletions config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

KERNEL = iso/boot/kernel.elf
DEBUG =# -ggdb3 #-Werror
MEMORY_SIZE=128M
MEMORY_SIZE?=128M
USE_SSE=true

COMPILER_DETECTOR_FLAGS = ""
Expand Down Expand Up @@ -220,7 +220,7 @@ SOURCES=\
OBJS = $(SOURCES:%.c=$(OBJ_DIRECTORY)/%.o)
DEPS = $(OBJS:%.o=%.d)

KERNEL_NEED = $(ASM) $(OBJS) $(CPP_CODE)
KERNEL_NEED = $(ASM) $(OBJS)

COMMON_FLAGS = -O0 -nostdlib -fno-stack-protector -fno-builtin -Ikernel/include/ -ffreestanding \
-Wall -Wno-div-by-zero -Wno-address-of-packed-member -Wno-implicit-function-declaration \
Expand Down
96 changes: 95 additions & 1 deletion kernel/asm/64bit_on_32bit.s
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Code from https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/i386/udivdi3.S
# Code from https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/i386/

.text
.balign 4
Expand Down Expand Up @@ -92,3 +92,97 @@ __udivdi3:
movl %ebx, %edx # rhi:alo = qlo*b + rlo with 0 ≤ rlo < b
popl %ebx #
retl # and return qhi:qlo




.text
.balign 4
.global __umoddi3
__umoddi3:
pushl %ebx
movl 20(%esp), %ebx
bsrl %ebx, %ecx
jz 9f

movl 16(%esp), %eax

shrl %cl, %eax
shrl %eax
notl %ecx
shll %cl, %ebx
orl %eax, %ebx
movl 12(%esp), %edx
movl 8(%esp), %eax
cmpl %ebx, %edx
jae 2f

divl %ebx

pushl %edi
notl %ecx
shrl %eax
shrl %cl, %eax
movl %eax, %edi
mull 20(%esp)
movl 12(%esp), %ebx
movl 16(%esp), %ecx
subl %eax, %ebx
sbbl %edx, %ecx
movl 24(%esp), %eax
imull %edi, %eax
subl %eax, %ecx

jnc 1f
addl 20(%esp), %ebx
adcl 24(%esp), %ecx
1: movl %ebx, %eax
movl %ecx, %edx

popl %edi
popl %ebx
retl


2:
subl %ebx, %edx
divl %ebx

pushl %edi
notl %ecx
shrl %eax
orl $0x80000000, %eax
shrl %cl, %eax
movl %eax, %edi
mull 20(%esp)
movl 12(%esp), %ebx
movl 16(%esp), %ecx
subl %eax, %ebx
sbbl %edx, %ecx
movl 24(%esp), %eax
imull %edi, %eax
subl %eax, %ecx

jnc 3f
addl 20(%esp), %ebx
adcl 24(%esp), %ecx
3: movl %ebx, %eax
movl %ecx, %edx

popl %edi
popl %ebx
retl


9:
movl 12(%esp), %eax
movl 16(%esp), %ecx
xorl %edx, %edx
divl %ecx
movl %eax, %ebx
movl 8(%esp), %eax
divl %ecx
movl %edx, %eax
popl %ebx
xorl %edx, %edx
retl
193 changes: 128 additions & 65 deletions kernel/asm/switch_task.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,131 @@
.extern current_proc
.extern tss

.global task_switch

task_switch:
cli

pushf

push %ebx
push %esi
push %edi
push %ebp

# Save current thread
mov current_thread, %edx

# Save current thread's stack
mov %esp, 28(%edx)

# current_thread.list_item.next is next entry in list
mov 4(%edx), %ecx

# Set current_thread to next entry
mov %ecx, current_thread

# Load thread's stack
mov current_thread, %edx
mov 28(%edx), %esp

# Load stack_top to tss
mov 40(%edx), %eax
mov $tss, %edx
mov %eax, 4(%edx)

# WARNING: THIS CODE IS MAY BE BUGGY (NOT TESTED)
# IF YOU HAVING PROBLEMS WITH THIS CODE, PLEASE, REPORT IT TO NDRAEY
# OR REMOVE THESE FOKEN LINES UNTIL THE `popf` INSTRUCTION

# Load process' page directory
# Load our process structure
mov current_thread, %ebx
mov 12(%ebx), %eax
mov %eax, current_proc

mov current_proc, %ebx

# Load our page directory address
mov 12(%ebx), %ebx

mov %cr3, %eax
cmp %eax, %ebx

je .no_switch_pd

mov %ebx, %cr3

.no_switch_pd:

pop %ebp
pop %edi
pop %esi
pop %ebx

popf

ret
#.global task_switch
#
#task_switch:
# cli
#
# pushf
#
# push %ebx
# push %esi
# push %edi
# push %ebp
#
# # Save current thread
# mov current_thread, %edx
#
# # Save current thread's stack
# mov %esp, 28(%edx)
#
# # current_thread.list_item.next is next entry in list
# mov 4(%edx), %ecx
#
# # Set current_thread to next entry
# mov %ecx, current_thread
#
# # Load thread's stack
# mov current_thread, %edx
# mov 28(%edx), %esp
#
# # Load stack_top to tss
# mov 40(%edx), %eax
# mov $tss, %edx
# mov %eax, 4(%edx)
#
# # WARNING: THIS CODE IS MAY BE BUGGY (NOT TESTED)
# # IF YOU HAVING PROBLEMS WITH THIS CODE, PLEASE, REPORT IT TO NDRAEY
# # OR REMOVE THESE FOKEN LINES UNTIL THE `popf` INSTRUCTION
#
# # Load process' page directory
# # Load our process structure
# mov current_thread, %ebx
# mov 12(%ebx), %eax
# mov %eax, current_proc
#
# mov current_proc, %ebx
#
# # Load our page directory address
# mov 12(%ebx), %ebx
#
# mov %cr3, %eax
# cmp %eax, %ebx
#
# je .no_switch_pd
#
# mov %ebx, %cr3
#
# .no_switch_pd:
#
# pop %ebp
# pop %edi
# pop %esi
# pop %ebx
#
# popf
#
# ret


.global task_switch_v2
task_switch_v2:
cli

pushf
push %ebx
push %esi
push %edi
push %ebp

mov 24(%esp), %eax # Current task
mov 28(%esp), %ebx # Next task

cmp %eax, %ebx
je .no_need

# Save current thread's stack
mov %esp, 28(%eax)

# Set current_thread to next entry
mov %ebx, current_thread

# Load thread's stack
mov %ebx, %edx
mov 28(%edx), %esp

# Load stack_top to tss
mov 40(%edx), %eax
mov $tss, %edx
mov %eax, 4(%edx)

# Load process' page directory
# Load our process structure
mov current_thread, %ebx
mov 12(%ebx), %eax
mov %eax, current_proc

mov current_proc, %ebx

# Load our page directory address
mov 12(%ebx), %ebx

mov %cr3, %eax
cmp %eax, %ebx

je .no_need

mov %ebx, %cr3

# .a: jmp .a

.no_need:

pop %ebp
pop %edi
pop %esi
pop %ebx

popf

ret
2 changes: 2 additions & 0 deletions kernel/include/drv/disk/ahci.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ struct ahci_port_descriptor {

size_t fis_virt;
size_t fis_phys;

bool is_atapi;
};

void ahci_init();
Expand Down
16 changes: 8 additions & 8 deletions kernel/include/mem/pmm.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ extern size_t kernel_end;
#define PAGE_TABLE_INDEX_BITS 10
#define PAGE_TABLE_INDEX_MASK 0x3FF

#define PAGE_PRESENT (1 << 0)
#define PAGE_WRITEABLE (1 << 1)
#define PAGE_USER (1 << 2)
#define PAGE_WRITE_THROUGH (1 << 3)
#define PAGE_CACHE_DISABLE (1 << 4)
#define PAGE_ACCESSED (1 << 5)
#define PAGE_DIRTY (1 << 6)
#define PAGE_GLOBAL (1 << 8)
#define PAGE_PRESENT (1U << 0)
#define PAGE_WRITEABLE (1U << 1)
#define PAGE_USER (1U << 2)
#define PAGE_WRITE_THROUGH (1U << 3)
#define PAGE_CACHE_DISABLE (1U << 4)
#define PAGE_ACCESSED (1U << 5)
#define PAGE_DIRTY (1U << 6)
#define PAGE_GLOBAL (1U << 8)

#define PAGE_BITMAP_SIZE (131072)

Expand Down
Loading
Loading