diff --git a/sys/SConscript b/sys/SConscript index 6574f5b..f1545e5 100644 --- a/sys/SConscript +++ b/sys/SConscript @@ -47,7 +47,7 @@ src_arm64 = [ "arm64/mp.c", "arm64/pci.c", "arm64/pmap.c", - "arm64/support.S", + "arm64/copy.c", "arm64/switch.S", "arm64/thread.c", "arm64/timer.c", diff --git a/sys/arm64/copy.c b/sys/arm64/copy.c new file mode 100644 index 0000000..2c29af8 --- /dev/null +++ b/sys/arm64/copy.c @@ -0,0 +1,14 @@ +#include +#include + +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; +} \ No newline at end of file diff --git a/sys/arm64/include/paging.h b/sys/arm64/include/paging.h index eafd2c9..68f2d9f 100644 --- a/sys/arm64/include/paging.h +++ b/sys/arm64/include/paging.h @@ -68,7 +68,7 @@ _Static_assert(sizeof(struct vmpt) == PGSIZE); // #define VMPD_ATTR_CA_DEVICE (VMPD_ATTR_CA_MAKE(1ull)) #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 { paddr_t next; // the next hppte in the list diff --git a/sys/arm64/include/trap.h b/sys/arm64/include/trap.h index bcc1e62..9e1d3b7 100644 --- a/sys/arm64/include/trap.h +++ b/sys/arm64/include/trap.h @@ -121,5 +121,7 @@ typedef struct TrapFrame void Trap_Pop(struct TrapFrame * tf); +#define SPSR_EL1H_DAIF0 (0b101) + #endif /* __TRAP_H__ */ diff --git a/sys/arm64/machine.c b/sys/arm64/machine.c index 31c0b05..eeb4fa2 100644 --- a/sys/arm64/machine.c +++ b/sys/arm64/machine.c @@ -148,11 +148,7 @@ void Machine_Init() Sched_SetRunnable(thr); Critical_Exit(); - - while(1) { - kprintf("Main thread!\n"); - hlt(); - } + /* * Load the init processor */ diff --git a/sys/arm64/mrt/priv.c b/sys/arm64/mrt/priv.c index 1425f3b..6b28d72 100644 --- a/sys/arm64/mrt/priv.c +++ b/sys/arm64/mrt/priv.c @@ -19,9 +19,8 @@ IMPL_MRT(mrt_kenter) WMCR(MCREG_MTP, kmtp); // switch to kernel stack - struct CPUState * state = (struct CPUState *)SYSREG_GET(tpidr_el1); - const uint32_t id = state->id; - kstack_top = curProc[id]->kstack + PGSIZE; + const int coreid = SYSREG_GET(tpidr_el1); + kstack_top = curProc[coreid]->kstack + PGSIZE; const regval_t kcpl = MCPL_KERNEL; WMCR(MGREG_MCPL, kcpl); diff --git a/sys/arm64/support.S b/sys/arm64/support.S deleted file mode 100644 index 3949db0..0000000 --- a/sys/arm64/support.S +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Support Functions - */ - -#include -#include - -.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) - diff --git a/sys/arm64/thread.c b/sys/arm64/thread.c index 9a1df5b..529d5f3 100644 --- a/sys/arm64/thread.c +++ b/sys/arm64/thread.c @@ -27,12 +27,9 @@ void ThreadKThreadEntry(TrapFrame *tf) __NO_LOCK_ANALYSIS { Spinlock_Unlock(&schedLock); - kprintf("tf in entry = 0x%p, elr = 0x%lx\n", tf, tf->elr); Trap_Pop(tf); } -#define SPSR_EL1H_DAIF0 (0b101) - void Thread_SetupKThread(Thread *thr, kthread_entry f, 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->r0 = (uint64_t)tf; sf->_r0 = (uint64_t)tf; - kprintf("tf before entry = 0x%p\n", tf); // interrupt enable tf->spsr = SPSR_EL1H_DAIF0; // el1h, enable interrupts @@ -90,7 +86,7 @@ Thread_SetupUThread(Thread *thr, uintptr_t rip, uintptr_t arg) void 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 switchstack(&oldthr->arch.sp, newthr->arch.sp); } diff --git a/sys/arm64/trap.c b/sys/arm64/trap.c index 8bd7bfc..cf3e02c 100644 --- a/sys/arm64/trap.c +++ b/sys/arm64/trap.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -104,9 +105,18 @@ trap_entry(TrapFrame * tf) { // kprintf("Trap: psp = 0x%lx, pcpl = %lu\n", tf->psp, tf->pcpl); if (tf->type == T_TYPE_EXC) { - 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); + 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); + } + } } 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_Handler(tf->intid); } diff --git a/sys/include/elf_common.h b/sys/include/elf_common.h index c9292e2..ee8de08 100644 --- a/sys/include/elf_common.h +++ b/sys/include/elf_common.h @@ -237,6 +237,7 @@ typedef struct { #define EM_ARCA 109 /* Arca RISC Microprocessor. */ #define EM_UNICORE 110 /* Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University */ +#define EM_AARCH64 (0xb7) /* Non-standard or deprecated. */ #define EM_486 6 /* Intel i486. */ diff --git a/sys/kern/loader.c b/sys/kern/loader.c index 3640c15..eaadbc1 100644 --- a/sys/kern/loader.c +++ b/sys/kern/loader.c @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -43,10 +44,15 @@ Loader_CheckHeader(const Elf64_Ehdr *ehdr) return false; } +#if defined (__x86_64__) if (ehdr->e_machine != EM_AMD64) { - return false; + return false; } - +#elif defined(__aarch64__) + if (ehdr->e_machine != EM_AARCH64) { + return false; + } +#endif return true; } @@ -227,7 +233,9 @@ Loader_LoadInit() /* * 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); @@ -275,9 +283,12 @@ Loader_LoadInit() TrapFrame tf; memset(&tf, 0, sizeof(tf)); tf.elr = thr->proc->entrypoint; + tf.spsr = SPSR_EL1H_DAIF0; + tf.psp = sp; + tf.pcpl = MCPL_USER; tf.r0 = sp; - //Trap_Pop(&tf); + Trap_Pop(&tf); #endif /* diff --git a/sys/kern/sched.c b/sys/kern/sched.c index 803eee5..269fd47 100644 --- a/sys/kern/sched.c +++ b/sys/kern/sched.c @@ -214,7 +214,6 @@ Sched_Scheduler() curProc[CPU()] = next; next->schedState = SCHED_STATE_RUNNING; next->ctxSwitches++; - kprintf("sched: cur = %lu next = %lu\n", prev->tid, next->tid); if (prev->schedState == SCHED_STATE_RUNNING) { prev->schedState = SCHED_STATE_RUNNABLE;