summaryrefslogtreecommitdiff
path: root/kc
diff options
context:
space:
mode:
authorAda Christine <adachristine18@gmail.com>2022-01-16 03:27:38 +0000
committerAda Christine <adachristine18@gmail.com>2022-01-16 03:27:38 +0000
commit8fe5dedec3c03a59809d8970362200a1f7b033ce (patch)
tree3f31b2d70138f54ec9eb2189e63c3a5fd5127280 /kc
parent069a9bfecc4161a1a08b5116a5baf3189d8c0efa (diff)
hide needlessly exported symbols
Diffstat (limited to 'kc')
-rw-r--r--kc/component.ld2
-rw-r--r--kc/core/cpu/cpu_task.S45
-rw-r--r--kc/core/exceptions.S55
-rw-r--r--kc/entry_x86_64.S1
4 files changed, 89 insertions, 14 deletions
diff --git a/kc/component.ld b/kc/component.ld
index 3a48ae8..dc158f3 100644
--- a/kc/component.ld
+++ b/kc/component.ld
@@ -2,7 +2,7 @@ ENTRY(kc_entry)
SECTIONS
{
- PROVIDE(kc_image_base = .);
+ PROVIDE_HIDDEN(kc_image_base = .);
. = sizeof_headers;
diff --git a/kc/core/cpu/cpu_task.S b/kc/core/cpu/cpu_task.S
new file mode 100644
index 0000000..f2fb473
--- /dev/null
+++ b/kc/core/cpu/cpu_task.S
@@ -0,0 +1,45 @@
+/* thread switching routines for x86_64
+* adapted from:
+* https://wiki.osdev.org/Brendan%27s_Multi-tasking_Tutorial
+*
+*
+*/
+
+.hidden cpu_set_thread
+.global cpu_set_thread
+.type cpu_set_thread, @function
+cpu_set_thread:
+ /* x86_64 SysV ABI
+ * must save the following registers to the stack:
+ * rbx, rsp, rbp, r12, r13, r14, r15
+ * the following are the parameters for switching tasks
+ * rdi: pointer to control block of thread being switched to
+ * rsi: pointer to control block of the current thread
+ * rdx: pointer to this cpu's task state segment esp0 field
+ */
+ push %rbx
+ push %r12
+ push %r13
+ push %r14
+ push %r15
+ push %rbp
+ /* the current task will be in rsi */
+ mov %esp, 0(%rsi) // save the stack pointer for the current task
+ mov 0(%rdi),%rsp // new stack is the next's task's stack
+ mov 8(%rdi),%rbx // top of the next task's kernel stack
+ mov %rbx,0(%rdx) // save the esp0 to the tss
+ mov 16(%rdi),%rax // page map for the next task
+ mov %cr3, %rcx // in case we need to change the page map
+ cmp %rax,%rcx
+ je .Lfinish
+ mov %rax,%cr3 // if the page maps are not the same, reload
+.Lfinish:
+ pop %rbp // get all the registers off the new task's stack
+ pop %r15
+ pop %r14
+ pop %r13
+ pop %r12
+ pop %rbx
+ ret // leave into the new task
+.size cpu_set_thread, . - cpu_set_thread
+
diff --git a/kc/core/exceptions.S b/kc/core/exceptions.S
index 80d49ee..46b9947 100644
--- a/kc/core/exceptions.S
+++ b/kc/core/exceptions.S
@@ -1,8 +1,7 @@
.extern page_fault_handler
+.extern general_protection_handler
-.global page_fault_isr
-.type page_fault_isr, @function
-page_fault_isr:
+.macro ISR_STORE_REGISTERS
/* just in case something tried to be clever before */
cld
/* push param registers */
@@ -12,9 +11,6 @@ page_fault_isr:
push %rcx
push %r8
push %r9
- /* collect parameters for page fault handler */
- mov 48(%rsp), %rdi /* error code is first parameter */
- mov %cr2, %rsi /* PFLA is next parameter */
/* caller-saved registers */
push %rax
push %r10
@@ -26,13 +22,10 @@ page_fault_isr:
push %r13
push %r14
push %r15
- /* stack has to be aligned */
- mov %rsp, %rbp
- and $-16, %rsp
- call page_fault_handler
- mov %rbp, %rsp
- /* TODO: test for error code here and panic() */
- pop %r15
+.endm
+
+.macro ISR_RESTORE_REGISTERS
+ // restore all the registers
pop %r14
pop %r13
pop %r12
@@ -47,8 +40,44 @@ page_fault_isr:
pop %rdx
pop %rsi
pop %rdi
+.endm
+
+.macro ISR_CALL func
+ // align the stack for the call to the handler function
+ mov %rsp, %rbp
+ and $-16, %rsp
+ call \func
+ // restore stack pointer, possibly unaligned
+ mov %rbp, %rsp
+.endm
+
+.hidden page_fault_isr
+.global page_fault_isr
+.type page_fault_isr, @function
+page_fault_isr:
+ ISR_STORE_REGISTERS
+ /* collect parameters for page fault handler */
+ mov 48(%rsp), %rdi /* error code is first parameter */
+ mov %cr2, %rsi /* PFLA is next parameter */
+ /* stack has to be aligned */
+ ISR_CALL page_fault_handler
+ /* TODO: test for error code here and panic() */
+ ISR_RESTORE_REGISTERS
/* unwind the error code from the stack */
add $8, %rsp
/* leave the isr */
iretq
.size page_fault_isr, . - page_fault_isr
+
+.hidden general_protection_isr
+.global general_protection_isr
+.type general_protection_isr, @function
+general_protection_isr:
+ ISR_STORE_REGISTERS
+ // TODO: parameters?
+ ISR_CALL general_protection_handler
+ add $8, %rsp
+ ISR_RESTORE_REGISTERS
+ iretq
+.size general_protection_isr, . - general_protection_isr
+
diff --git a/kc/entry_x86_64.S b/kc/entry_x86_64.S
index 231953a..abea8ac 100644
--- a/kc/entry_x86_64.S
+++ b/kc/entry_x86_64.S
@@ -4,6 +4,7 @@
.extern _DYNAMIC
.extern kc_image_base
+.hidden kc_entry
.global kc_entry
.type kc_entry, @function
kc_entry: