proc: fix errors, enter userspace
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
b279d0a06a
commit
c7fa9a75fa
15 changed files with 86 additions and 8 deletions
|
|
@ -61,4 +61,6 @@ void arch_ap_startup()
|
|||
|
||||
struct cpu_local *this_local = &cpu_local_arr[id];
|
||||
wrmsr(IA32_GS_BASE, (u64)this_local);
|
||||
|
||||
tss_init(id, this_local);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,6 @@ void cr4_write(u64 cr4);
|
|||
u64 cr4_read();
|
||||
void lidt();
|
||||
void lgdt();
|
||||
void ltr();
|
||||
void ltr(u16 index);
|
||||
|
||||
#endif /* _REG_H_ */
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@
|
|||
#define GDT_SEGMENT_DATA_RING0 0x30
|
||||
#define GDT_SEGMENT_CODE_RING3 0x38
|
||||
#define GDT_SEGMENT_DATA_RING3 0x40
|
||||
#define GDT_SEGMENT_TSS 0x48
|
||||
|
||||
#define GATE_INTR 0x8E
|
||||
#define GATE_TRAP 0x8F
|
||||
|
|
@ -21,6 +22,8 @@
|
|||
|
||||
#include <kernel/common.h>
|
||||
|
||||
struct cpu_local;
|
||||
|
||||
struct gdt_desc {
|
||||
uint16_t limit;
|
||||
uint16_t base;
|
||||
|
|
@ -87,5 +90,6 @@ extern struct irq_desc __idt[64];
|
|||
|
||||
void gdt_init();
|
||||
void idt_init();
|
||||
void tss_init(int cpu_id, struct cpu_local *local);
|
||||
|
||||
#endif /* _DESCRIPTOR_H_ */
|
||||
|
|
|
|||
34
amd64/gdt.c
34
amd64/gdt.c
|
|
@ -1,9 +1,14 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <arch/proc.h>
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <lib/string.h>
|
||||
|
||||
#include "descriptor.h"
|
||||
#include "arch/reg.h"
|
||||
|
||||
#define TSS_STACK_SIZE 0x4000
|
||||
|
||||
static size_t gdt_index = 0;
|
||||
|
||||
void gdt_insert(uint8_t offset, uint8_t access, uint8_t flags)
|
||||
|
|
@ -38,3 +43,32 @@ void gdt_init()
|
|||
|
||||
krdy("GDT initialized\n");
|
||||
}
|
||||
|
||||
void tss_init(int cpu_id, struct cpu_local *local)
|
||||
{
|
||||
int gdt_idx = 9 + cpu_id * 2;
|
||||
uint16_t sel = gdt_idx * 8;
|
||||
|
||||
char *tss_stack = kalloc(TSS_STACK_SIZE, ALLOC_KERN);
|
||||
if (!tss_stack)
|
||||
panic("failed to alloc tss stack for cpu %d\n", cpu_id);
|
||||
|
||||
struct tss *tss = &local->tss;
|
||||
|
||||
memset(tss, 0, sizeof(*tss));
|
||||
tss->rsp0 = (u64)tss_stack + TSS_STACK_SIZE;
|
||||
|
||||
uint64_t addr = (uint64_t)tss;
|
||||
struct gdt_desc_sys *desc = (struct gdt_desc_sys *)&__gdt[gdt_idx];
|
||||
|
||||
desc->limit = sizeof(struct tss) - 1;
|
||||
desc->base = addr & 0xFFFF;
|
||||
desc->base_2 = (addr >> 16) & 0xFF;
|
||||
desc->base_3 = (addr >> 24) & 0xFF;
|
||||
desc->base_4 = addr >> 32;
|
||||
desc->access = 0x89;
|
||||
desc->flags = 0x00;
|
||||
desc->reserved = 0;
|
||||
|
||||
ltr(sel);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#ifndef _ARCH_PROC_H_
|
||||
#define _ARCH_PROC_H_
|
||||
|
||||
#include <descriptor.h>
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/types.h>
|
||||
|
||||
|
|
@ -11,6 +12,7 @@ struct cpu_local {
|
|||
vaddr_t kernel_rsp;
|
||||
vaddr_t user_rsp;
|
||||
u32 cpu_id;
|
||||
struct tss tss;
|
||||
} PACKED;
|
||||
|
||||
struct pt_regs {
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ void enter_userspace_init(struct thread *thread, uaddr_t entry)
|
|||
|
||||
void *kstack = kalloc(PAGE_SIZE, ALLOC_KERN);
|
||||
tls->kernel_rsp = (vaddr_t)kstack + PAGE_SIZE;
|
||||
tls->tss.rsp0 = tls->kernel_rsp;
|
||||
|
||||
pgd_load(thread->proc->pgd);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue