1 #include <lunaix/process.h>
2 #include <lunaix/hart_state.h>
3 #include <lunaix/mm/vmm.h>
4 #include <klibc/string.h>
6 #include <sys/mm/mempart.h>
9 volatile struct x86_tss _tss = { .link = 0,
14 install_hart_transition(ptr_t vm_mnt, struct hart_transition* ht)
17 if (!vmm_lookupat(vm_mnt, ht->inject, &pte)) {
21 mount_page(PG_MOUNT_4, pte_paddr(pte));
23 ptr_t mount_inject = PG_MOUNT_4 + va_offset(ht->inject);
24 memcpy((void*)mount_inject, &ht->transfer, sizeof(ht->transfer));
26 unmount_page(PG_MOUNT_4);
31 hart_prepare_transition(struct hart_transition* ht,
32 ptr_t kstack_tp, ptr_t ustack_pt,
33 ptr_t entry, bool to_user)
35 ptr_t offset = (ptr_t)&ht->transfer.eret - (ptr_t)&ht->transfer;
36 ht->inject = align_stack(kstack_tp - sizeof(ht->transfer));
38 ht->transfer.state = (struct hart_state){
45 .execp = (struct exec_param*)(ht->inject + offset)
48 int code_seg = KCODE_SEG, data_seg = KDATA_SEG;
49 int mstate = cpu_ldstate();
51 code_seg = UCODE_SEG, data_seg = UDATA_SEG;
52 mstate |= 0x200; // enable interrupt
55 ht->transfer.eret = (struct exec_param) {
56 .cs = code_seg, .eip = entry,
57 .ss = data_seg, .esp = align_stack(ustack_pt),