[25] | 1 | /* |
---|
[234] | 2 | * hal_context.c - Implementation of Thread Context API for x86_64 |
---|
[25] | 3 | * |
---|
[234] | 4 | * Copyright (c) 2017 Maxime Villard |
---|
| 5 | * |
---|
[25] | 6 | * This file is part of ALMOS-MKH. |
---|
| 7 | * |
---|
[234] | 8 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
[25] | 9 | * under the terms of the GNU General Public License as published by |
---|
| 10 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 11 | * |
---|
[234] | 12 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
[25] | 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 15 | * General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
[234] | 18 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
[25] | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 20 | */ |
---|
| 21 | |
---|
[457] | 22 | #include <hal_kernel_types.h> |
---|
[25] | 23 | #include <memcpy.h> |
---|
| 24 | #include <thread.h> |
---|
| 25 | #include <string.h> |
---|
| 26 | #include <process.h> |
---|
| 27 | #include <printk.h> |
---|
| 28 | #include <vmm.h> |
---|
| 29 | #include <core.h> |
---|
| 30 | #include <cluster.h> |
---|
| 31 | |
---|
| 32 | #include <hal_context.h> |
---|
[193] | 33 | #include <hal_kentry.h> |
---|
[48] | 34 | #include <hal_internal.h> |
---|
[193] | 35 | #include <hal_segmentation.h> |
---|
[48] | 36 | |
---|
[193] | 37 | error_t hal_cpu_context_create(struct thread_s *thread) |
---|
[25] | 38 | { |
---|
[335] | 39 | hal_trapframe_t *tf; |
---|
| 40 | hal_cpu_context_t *ctx; |
---|
[339] | 41 | uint64_t kstacktop; |
---|
[193] | 42 | kmem_req_t req; |
---|
| 43 | |
---|
[339] | 44 | kstacktop = (uint64_t)thread->k_stack_base + thread->k_stack_size; |
---|
| 45 | |
---|
[193] | 46 | /* allocate memory for cpu_context */ |
---|
| 47 | req.type = KMEM_CPU_CTX; |
---|
| 48 | req.size = sizeof(hal_cpu_context_t); |
---|
| 49 | req.flags = AF_KERNEL | AF_ZERO; |
---|
| 50 | |
---|
[335] | 51 | ctx = (hal_cpu_context_t *)kmem_alloc(&req); |
---|
| 52 | if (ctx == NULL) |
---|
[193] | 53 | return ENOMEM; |
---|
| 54 | |
---|
| 55 | /* set cpu context pointer in thread */ |
---|
[335] | 56 | thread->cpu_context = (void *)ctx; |
---|
[193] | 57 | |
---|
[335] | 58 | /* |
---|
| 59 | * Build the context |
---|
| 60 | */ |
---|
[339] | 61 | ctx->ctx_rsp0 = kstacktop & ~0xF; |
---|
| 62 | ctx->ctx_tf = (uint64_t)&ctx->ctx_hidden_tf; |
---|
[359] | 63 | ctx->ctx_intr = INTRS_ENABLED; |
---|
[193] | 64 | |
---|
[335] | 65 | /* |
---|
| 66 | * Build the trap frame |
---|
| 67 | */ |
---|
[339] | 68 | tf = &ctx->ctx_hidden_tf; |
---|
[361] | 69 | tf->tf_rdi = (uint64_t)thread->entry_args; |
---|
[335] | 70 | tf->tf_gs = GDT_FIXED_SEL(GDT_UDATA_SEL, SEL_UPL); |
---|
| 71 | tf->tf_fs = GDT_FIXED_SEL(GDT_UDATA_SEL, SEL_UPL); |
---|
| 72 | tf->tf_es = GDT_FIXED_SEL(GDT_UDATA_SEL, SEL_UPL); |
---|
| 73 | tf->tf_ds = GDT_FIXED_SEL(GDT_UDATA_SEL, SEL_UPL); |
---|
| 74 | tf->tf_rip = (uint64_t)thread->entry_func; |
---|
| 75 | tf->tf_rflags = PSL_USERSET; |
---|
| 76 | |
---|
[193] | 77 | if (thread->type == THREAD_USER) { |
---|
[335] | 78 | tf->tf_cs = GDT_FIXED_SEL(GDT_UCODE_SEL, SEL_UPL); |
---|
[342] | 79 | tf->tf_ss = GDT_FIXED_SEL(GDT_UDATA_SEL, SEL_UPL); |
---|
[335] | 80 | tf->tf_rsp = ((uint64_t)thread->u_stack_base) + |
---|
[193] | 81 | thread->u_stack_size; |
---|
| 82 | } else { |
---|
[335] | 83 | tf->tf_cs = GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL); |
---|
[342] | 84 | tf->tf_ss = GDT_FIXED_SEL(GDT_KDATA_SEL, SEL_KPL); |
---|
[339] | 85 | tf->tf_rsp = kstacktop; |
---|
[193] | 86 | } |
---|
| 87 | |
---|
[48] | 88 | return 0; |
---|
[25] | 89 | } |
---|
| 90 | |
---|
[335] | 91 | error_t hal_cpu_context_copy(thread_t *dst, thread_t *src) |
---|
[25] | 92 | { |
---|
[48] | 93 | x86_panic((char *)__func__); |
---|
| 94 | return 0; |
---|
[25] | 95 | } |
---|
| 96 | |
---|
[335] | 97 | void hal_cpu_context_destroy(thread_t *thread) |
---|
[25] | 98 | { |
---|
[48] | 99 | x86_panic((char *)__func__); |
---|
[25] | 100 | } |
---|
| 101 | |
---|
[335] | 102 | void hal_cpu_context_switch(thread_t *old, thread_t *new) |
---|
[25] | 103 | { |
---|
[360] | 104 | process_t *oldproc = old->process; |
---|
| 105 | process_t *newproc = new->process; |
---|
| 106 | |
---|
[342] | 107 | curtls()->tls_thr = new; |
---|
[339] | 108 | |
---|
| 109 | /* Switch the VM space */ |
---|
[360] | 110 | if (newproc != oldproc) { |
---|
[368] | 111 | lcr3((uint64_t)newproc->vmm.gpt.ppn << CONFIG_PPM_PAGE_SHIFT); |
---|
[360] | 112 | } |
---|
[339] | 113 | |
---|
| 114 | /* Switch the CPU context */ |
---|
| 115 | cpu_context_switch(old->cpu_context, new->cpu_context); |
---|
[25] | 116 | } |
---|
| 117 | |
---|
[335] | 118 | void hal_cpu_context_load( thread_t * thread ) |
---|
[25] | 119 | { |
---|
[48] | 120 | x86_panic((char *)__func__); |
---|
[25] | 121 | } |
---|
| 122 | |
---|
[335] | 123 | error_t hal_fpu_context_create( thread_t * thread ) |
---|
[25] | 124 | { |
---|
[368] | 125 | /* FPU not implemented yet */ |
---|
[335] | 126 | return 0; |
---|
[25] | 127 | } |
---|
| 128 | |
---|
[335] | 129 | error_t hal_fpu_context_copy( thread_t * dst, |
---|
| 130 | thread_t * src ) |
---|
[25] | 131 | { |
---|
[48] | 132 | x86_panic((char *)__func__); |
---|
[335] | 133 | return 0; |
---|
[48] | 134 | } |
---|
[25] | 135 | |
---|
[335] | 136 | void hal_fpu_context_destroy( thread_t * thread ) |
---|
[25] | 137 | { |
---|
[48] | 138 | x86_panic((char *)__func__); |
---|
[25] | 139 | } |
---|
| 140 | |
---|
| 141 | void hal_fpu_context_save( thread_t * thread ) |
---|
| 142 | { |
---|
[48] | 143 | x86_panic((char *)__func__); |
---|
[25] | 144 | } |
---|
| 145 | |
---|
| 146 | void hal_fpu_context_restore( thread_t * thread ) |
---|
| 147 | { |
---|
[48] | 148 | x86_panic((char *)__func__); |
---|
[25] | 149 | } |
---|
| 150 | |
---|
| 151 | void hal_fpu_context_dup( xptr_t dst, |
---|
| 152 | xptr_t src ) |
---|
| 153 | { |
---|
[48] | 154 | x86_panic((char *)__func__); |
---|
[25] | 155 | } |
---|
| 156 | |
---|