[amd64] Fix RSP0 not getting saved (threads restarting), add int $0x80

This commit is contained in:
Mark
2019-09-20 15:07:19 +03:00
parent 49928b0a2b
commit c5ffd32898
6 changed files with 55 additions and 8 deletions
+2 -1
View File
@@ -24,7 +24,8 @@ OBJS+=$(O)/arch/amd64/kernel.o \
$(O)/arch/amd64/mm/heap.o \
$(O)/arch/amd64/mm/vmalloc.o \
$(O)/arch/amd64/sys/thread.o \
$(O)/arch/amd64/ctxsw.o
$(O)/arch/amd64/ctxsw.o \
$(O)/arch/amd64/sys/syscall.o
kernel_OBJS=$(O)/arch/amd64/entry.o \
$(OBJS)
kernel_LINKER=$(S)/arch/amd64/link.ld
+13 -7
View File
@@ -1,23 +1,31 @@
#include "sys/debug.h"
#include "sys/thread.h"
#define KSTACK_SIZE 32768
// TODO: move to sched.c
thread_t *amd64_thread_current = NULL;
thread_t *amd64_thread_first = NULL;
thread_t *amd64_thread_last = NULL;
static thread_t threads[2];
static char kstacks[2 * 16384];
static char kstacks[2 * KSTACK_SIZE];
static void kthread0(void) {
while (1) {
asm volatile ("int $0x80");
kdebug("A\n");
for (int i = 0; i < 10000000; ++i);
}
}
static void kthread1(void) {
kdebug("Start\n");
while (1) {
kdebug("B\n");
for (int i = 0; i < 10000000; ++i);
kdebug("C\n");
for (int i = 0; i < 10000000; ++i);
}
}
@@ -37,16 +45,14 @@ void amd64_ctx_add(thread_t *t) {
}
void amd64_init_test_threads(void) {
amd64_thread_current = NULL;
for (size_t i = 0; i < (sizeof(threads) / sizeof(threads[0])); ++i) {
thread_info_init(&threads[i].info);
threads[i].kstack_base = (uintptr_t) &kstacks[i * 16384];
threads[i].kstack_size = 16384;
threads[i].kstack_base = (uintptr_t) &kstacks[i * KSTACK_SIZE];
threads[i].kstack_size = KSTACK_SIZE;
threads[i].kstack_ptr = threads[i].kstack_base + threads[i].kstack_size - sizeof(amd64_thread_context_t);
kdebug("base = %p, kstacks = %p, ptr = %p\n", threads[i].kstack_base,
kstacks,
threads[i].kstack_ptr);
threads[i].info.flags = THREAD_KERNEL;
+4
View File
@@ -53,6 +53,8 @@ extern void amd64_isr_29();
extern void amd64_isr_30();
extern void amd64_isr_31();
extern void amd64_isr_syscall();
// 8259 PIC IRQs
extern void amd64_irq_0(); // Timer
extern void amd64_irq_1(); // TODO
@@ -138,6 +140,8 @@ void amd64_idt_init(void) {
amd64_idt_set(30, (uintptr_t) amd64_isr_30, 0x08, IDT_FLG_P | IDT_FLG_R0 | IDT_FLG_INT32);
amd64_idt_set(31, (uintptr_t) amd64_isr_31, 0x08, IDT_FLG_P | IDT_FLG_R0 | IDT_FLG_INT32);
amd64_idt_set(0x80, (uintptr_t) amd64_isr_syscall, 0x08, IDT_FLG_P | IDT_FLG_R0 | IDT_FLG_INT32);
amd64_idt_set(IRQ_BASE + 0 , (uintptr_t) amd64_irq_0 , 0x08, IDT_FLG_P | IDT_FLG_R0 | IDT_FLG_INT32);
amd64_idt_set(IRQ_BASE + 1 , (uintptr_t) amd64_irq_1 , 0x08, IDT_FLG_P | IDT_FLG_R0 | IDT_FLG_INT32);
amd64_idt_set(IRQ_BASE + 2 , (uintptr_t) amd64_irq_2 , 0x08, IDT_FLG_P | IDT_FLG_R0 | IDT_FLG_INT32);
+11
View File
@@ -1,6 +1,8 @@
#include "arch/amd64/hw/int_macros.inc"
.section .text
.extern amd64_err_num
// For legacy system call interface (via int $0x80)
.global amd64_isr_syscall
__isr_generic:
cli
@@ -71,3 +73,12 @@ __isr_nerr 28
__isr_nerr 29
__isr_yerr 30
__isr_nerr 31
amd64_isr_syscall:
cli
__int_push_ctx
call amd64_syscall_int
__int_pop_ctx
iretq
+8
View File
@@ -7,6 +7,14 @@ amd64_irq_0:
__int_push_ctx
// Store current RSP
movq %rsp, %rax
movq amd64_thread_current(%rip), %rdi
test %rdi, %rdi
jz 1f
movq %rax, (%rdi)
1:
movq amd64_timer_tick(%rip), %rax
callq *%rax
+17
View File
@@ -0,0 +1,17 @@
#include "sys/thread.h"
#include "sys/debug.h"
/**
* @brief Interrupt-based syscall entry
*/
void amd64_syscall_int(void) {
// TODO: write something meaningful here
uint64_t cnt = 1000000;
kdebug("Waiting\n");
while (cnt != 1) {
asm volatile ("sti");
--cnt;
}
kdebug("Waking up\n");
}