userspace

This commit is contained in:
oscar 2024-11-20 07:00:12 +08:00
parent db8c734f4e
commit a87b119b53
12 changed files with 50 additions and 48 deletions

View File

@ -47,7 +47,7 @@ src_arm64 = [
"arm64/mp.c", "arm64/mp.c",
"arm64/pci.c", "arm64/pci.c",
"arm64/pmap.c", "arm64/pmap.c",
"arm64/support.S", "arm64/copy.c",
"arm64/switch.S", "arm64/switch.S",
"arm64/thread.c", "arm64/thread.c",
"arm64/timer.c", "arm64/timer.c",

14
sys/arm64/copy.c Normal file
View File

@ -0,0 +1,14 @@
#include <stdint.h>
#include <string.h>
int copy_unsafe(void *to_addr, void *from_addr, uintptr_t len)
{
memcpy(to_addr, from_addr, len);
return 0;
}
int copystr_unsafe(void *to_addr, void *from_addr, uintptr_t len)
{
memcpy(to_addr, from_addr, len);
return 0;
}

View File

@ -68,7 +68,7 @@ _Static_assert(sizeof(struct vmpt) == PGSIZE);
// #define VMPD_ATTR_CA_DEVICE (VMPD_ATTR_CA_MAKE(1ull)) // #define VMPD_ATTR_CA_DEVICE (VMPD_ATTR_CA_MAKE(1ull))
#define MTP_KERNEL (0b0010 | (0b0000 << 4) | (0b0000 << 8) | (0b0000 << 12)) #define MTP_KERNEL (0b0010 | (0b0000 << 4) | (0b0000 << 8) | (0b0000 << 12))
#define MTP_USER (0b0010 | (0b0000 << 4) | (0b0011 << 8) | (0b0010 << 12)) #define MTP_USER (0b0010 | (0b0000 << 4) | (0b0000 << 8) | (0b0000 << 12))
struct vmpd { struct vmpd {
paddr_t next; // the next hppte in the list paddr_t next; // the next hppte in the list

View File

@ -121,5 +121,7 @@ typedef struct TrapFrame
void Trap_Pop(struct TrapFrame * tf); void Trap_Pop(struct TrapFrame * tf);
#define SPSR_EL1H_DAIF0 (0b101)
#endif /* __TRAP_H__ */ #endif /* __TRAP_H__ */

View File

@ -149,10 +149,6 @@ void Machine_Init()
Critical_Exit(); Critical_Exit();
while(1) {
kprintf("Main thread!\n");
hlt();
}
/* /*
* Load the init processor * Load the init processor
*/ */

View File

@ -19,9 +19,8 @@ IMPL_MRT(mrt_kenter)
WMCR(MCREG_MTP, kmtp); WMCR(MCREG_MTP, kmtp);
// switch to kernel stack // switch to kernel stack
struct CPUState * state = (struct CPUState *)SYSREG_GET(tpidr_el1); const int coreid = SYSREG_GET(tpidr_el1);
const uint32_t id = state->id; kstack_top = curProc[coreid]->kstack + PGSIZE;
kstack_top = curProc[id]->kstack + PGSIZE;
const regval_t kcpl = MCPL_KERNEL; const regval_t kcpl = MCPL_KERNEL;
WMCR(MGREG_MCPL, kcpl); WMCR(MGREG_MCPL, kcpl);

View File

@ -1,26 +0,0 @@
/*
* Support Functions
*/
#include <errno.h>
#include <machine/asm.h>
.text
// copy_unsafe(to, from, len)
FUNC_BEGIN(copy_unsafe)
.globl copy_unsafe_done
copy_unsafe_done:
.globl copy_unsafe_fault
copy_unsafe_fault:
FUNC_END(copy_unsafe)
// copystr_unsafe(to, from, len)
FUNC_BEGIN(copystr_unsafe)
.globl copystr_unsafe_done
copystr_unsafe_done:
.globl copystr_unsafe_fault
copystr_unsafe_fault:
.globl copystr_unsafe_toolong
FUNC_END(copystr_unsafe)

View File

@ -27,12 +27,9 @@ void
ThreadKThreadEntry(TrapFrame *tf) __NO_LOCK_ANALYSIS ThreadKThreadEntry(TrapFrame *tf) __NO_LOCK_ANALYSIS
{ {
Spinlock_Unlock(&schedLock); Spinlock_Unlock(&schedLock);
kprintf("tf in entry = 0x%p, elr = 0x%lx\n", tf, tf->elr);
Trap_Pop(tf); Trap_Pop(tf);
} }
#define SPSR_EL1H_DAIF0 (0b101)
void void
Thread_SetupKThread(Thread *thr, kthread_entry f, Thread_SetupKThread(Thread *thr, kthread_entry f,
uintptr_t arg1, uintptr_t arg2, uintptr_t arg3) uintptr_t arg1, uintptr_t arg2, uintptr_t arg3)
@ -53,7 +50,6 @@ Thread_SetupKThread(Thread *thr, kthread_entry f,
sf->lr = (uint64_t)&ThreadKThreadEntry; sf->lr = (uint64_t)&ThreadKThreadEntry;
sf->r0 = (uint64_t)tf; sf->r0 = (uint64_t)tf;
sf->_r0 = (uint64_t)tf; sf->_r0 = (uint64_t)tf;
kprintf("tf before entry = 0x%p\n", tf);
// interrupt enable // interrupt enable
tf->spsr = SPSR_EL1H_DAIF0; // el1h, enable interrupts tf->spsr = SPSR_EL1H_DAIF0; // el1h, enable interrupts
@ -90,7 +86,7 @@ Thread_SetupUThread(Thread *thr, uintptr_t rip, uintptr_t arg)
void void
Thread_SwitchArch(Thread *oldthr, Thread *newthr) Thread_SwitchArch(Thread *oldthr, Thread *newthr)
{ {
kprintf("switch stack! oldthr sp = 0x%lx, newthr sp = 0x%lx\n", oldthr->arch.sp, newthr->arch.sp); //kprintf("switch stack! oldthr sp = 0x%lx, newthr sp = 0x%lx\n", oldthr->arch.sp, newthr->arch.sp);
// Jump to trapframe // Jump to trapframe
switchstack(&oldthr->arch.sp, newthr->arch.sp); switchstack(&oldthr->arch.sp, newthr->arch.sp);
} }

View File

@ -4,6 +4,7 @@
#include <sys/kassert.h> #include <sys/kassert.h>
#include <sys/irq.h> #include <sys/irq.h>
#include <sys/kdebug.h> #include <sys/kdebug.h>
#include <sys/syscall.h>
#include <machine/cpuop.h> #include <machine/cpuop.h>
#include <machine/gic.h> #include <machine/gic.h>
@ -104,9 +105,18 @@ trap_entry(TrapFrame * tf)
{ {
// kprintf("Trap: psp = 0x%lx, pcpl = %lu\n", tf->psp, tf->pcpl); // kprintf("Trap: psp = 0x%lx, pcpl = %lu\n", tf->psp, tf->pcpl);
if (tf->type == T_TYPE_EXC) { if (tf->type == T_TYPE_EXC) {
const uint64_t ec = (tf->esr >> 26) & 0b111111;
switch (ec) {
case 0b010101: {
tf->r0 = Syscall_Entry(tf->r0, tf->r1, tf->r2, tf->r3, tf->r4, tf->r5);
break;
}
default: {
Panic("Unexpected exception: Type = 0x%lx, IntID = 0x%lx, ESR = 0x%lx, FAR = 0x%lx, ELR = 0x%lx, SPSR = 0x%lx.\n", tf->type, tf->intid, tf->esr, tf->far, tf->elr, tf->spsr); Panic("Unexpected exception: Type = 0x%lx, IntID = 0x%lx, ESR = 0x%lx, FAR = 0x%lx, ELR = 0x%lx, SPSR = 0x%lx.\n", tf->type, tf->intid, tf->esr, tf->far, tf->elr, tf->spsr);
}
}
} else { } else {
kprintf("IRQ: Type = 0x%lx, IntID = 0x%lx, ESR = 0x%lx, FAR = 0x%lx, ELR = 0x%lx, SPSR = 0x%lx.\n", tf->type, tf->intid, tf->esr, tf->far, tf->elr, tf->spsr); // kprintf("IRQ: Type = 0x%lx, IntID = 0x%lx, ESR = 0x%lx, FAR = 0x%lx, ELR = 0x%lx, SPSR = 0x%lx.\n", tf->type, tf->intid, tf->esr, tf->far, tf->elr, tf->spsr);
// irq // irq
IRQ_Handler(tf->intid); IRQ_Handler(tf->intid);
} }

View File

@ -237,6 +237,7 @@ typedef struct {
#define EM_ARCA 109 /* Arca RISC Microprocessor. */ #define EM_ARCA 109 /* Arca RISC Microprocessor. */
#define EM_UNICORE 110 /* Microprocessor series from PKU-Unity Ltd. #define EM_UNICORE 110 /* Microprocessor series from PKU-Unity Ltd.
and MPRC of Peking University */ and MPRC of Peking University */
#define EM_AARCH64 (0xb7)
/* Non-standard or deprecated. */ /* Non-standard or deprecated. */
#define EM_486 6 /* Intel i486. */ #define EM_486 6 /* Intel i486. */

View File

@ -17,6 +17,7 @@
#include <machine/trap.h> #include <machine/trap.h>
#include <machine/pmap.h> #include <machine/pmap.h>
#include <machine/metal.h>
#include <sys/thread.h> #include <sys/thread.h>
#include <sys/spinlock.h> #include <sys/spinlock.h>
#include <sys/loader.h> #include <sys/loader.h>
@ -43,10 +44,15 @@ Loader_CheckHeader(const Elf64_Ehdr *ehdr)
return false; return false;
} }
#if defined (__x86_64__)
if (ehdr->e_machine != EM_AMD64) { if (ehdr->e_machine != EM_AMD64) {
return false; return false;
} }
#elif defined(__aarch64__)
if (ehdr->e_machine != EM_AARCH64) {
return false;
}
#endif
return true; return true;
} }
@ -227,7 +233,9 @@ Loader_LoadInit()
/* /*
* Load init binary * Load init binary
*/ */
Loader_Load(thr, initvn, pg, 1024); if (!Loader_Load(thr, initvn, pg, 1024)) {
Panic("Failed to load init.\n");
}
VFS_Close(initvn); VFS_Close(initvn);
@ -275,9 +283,12 @@ Loader_LoadInit()
TrapFrame tf; TrapFrame tf;
memset(&tf, 0, sizeof(tf)); memset(&tf, 0, sizeof(tf));
tf.elr = thr->proc->entrypoint; tf.elr = thr->proc->entrypoint;
tf.spsr = SPSR_EL1H_DAIF0;
tf.psp = sp;
tf.pcpl = MCPL_USER;
tf.r0 = sp; tf.r0 = sp;
//Trap_Pop(&tf); Trap_Pop(&tf);
#endif #endif
/* /*

View File

@ -214,7 +214,6 @@ Sched_Scheduler()
curProc[CPU()] = next; curProc[CPU()] = next;
next->schedState = SCHED_STATE_RUNNING; next->schedState = SCHED_STATE_RUNNING;
next->ctxSwitches++; next->ctxSwitches++;
kprintf("sched: cur = %lu next = %lu\n", prev->tid, next->tid);
if (prev->schedState == SCHED_STATE_RUNNING) { if (prev->schedState == SCHED_STATE_RUNNING) {
prev->schedState = SCHED_STATE_RUNNABLE; prev->schedState = SCHED_STATE_RUNNABLE;