1 | /* |
---|
2 | * hal_context.c - Implementation of Thread Context API for x86_64 |
---|
3 | * |
---|
4 | * Copyright (c) 2017 Maxime Villard |
---|
5 | * |
---|
6 | * This file is part of ALMOS-MKH. |
---|
7 | * |
---|
8 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
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 | * |
---|
12 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
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 |
---|
18 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
20 | */ |
---|
21 | |
---|
22 | #include <hal_types.h> |
---|
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> |
---|
33 | #include <hal_kentry.h> |
---|
34 | #include <hal_internal.h> |
---|
35 | #include <hal_segmentation.h> |
---|
36 | |
---|
37 | error_t hal_cpu_context_create(struct thread_s *thread) |
---|
38 | { |
---|
39 | hal_cpu_context_t *context; |
---|
40 | kmem_req_t req; |
---|
41 | |
---|
42 | /* allocate memory for cpu_context */ |
---|
43 | req.type = KMEM_CPU_CTX; |
---|
44 | req.size = sizeof(hal_cpu_context_t); |
---|
45 | req.flags = AF_KERNEL | AF_ZERO; |
---|
46 | |
---|
47 | context = (hal_cpu_context_t *)kmem_alloc(&req); |
---|
48 | if (context == NULL) |
---|
49 | return ENOMEM; |
---|
50 | |
---|
51 | /* set cpu context pointer in thread */ |
---|
52 | thread->cpu_context = (void *)context; |
---|
53 | |
---|
54 | /* build the context */ |
---|
55 | /* tf_gs */ |
---|
56 | /* tf_fs */ |
---|
57 | context->tf_es = GDT_FIXED_SEL(GDT_UDATA_SEL, SEL_UPL); |
---|
58 | context->tf_ds = GDT_FIXED_SEL(GDT_UDATA_SEL, SEL_UPL); |
---|
59 | context->tf_rip = (uint64_t)thread->entry_func; |
---|
60 | context->tf_rflags = PSL_USERSET; |
---|
61 | |
---|
62 | if (thread->type == THREAD_USER) { |
---|
63 | context->tf_cs = GDT_FIXED_SEL(GDT_UCODE_SEL, SEL_UPL); |
---|
64 | context->tf_rsp = ((uint64_t)thread->u_stack_base) + |
---|
65 | thread->u_stack_size; |
---|
66 | } else { |
---|
67 | context->tf_cs = GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL); |
---|
68 | context->tf_rsp = ((uint64_t)thread->k_stack_base) + |
---|
69 | thread->k_stack_size; |
---|
70 | } |
---|
71 | |
---|
72 | return 0; |
---|
73 | } |
---|
74 | |
---|
75 | error_t hal_cpu_context_copy( thread_t * dst, |
---|
76 | thread_t * src ) |
---|
77 | { |
---|
78 | x86_panic((char *)__func__); |
---|
79 | return 0; |
---|
80 | } |
---|
81 | |
---|
82 | void hal_cpu_context_destroy( thread_t * thread ) |
---|
83 | { |
---|
84 | x86_panic((char *)__func__); |
---|
85 | } |
---|
86 | |
---|
87 | error_t hal_fpu_context_create( thread_t * thread ) |
---|
88 | { |
---|
89 | x86_panic((char *)__func__); |
---|
90 | return 0; |
---|
91 | } |
---|
92 | |
---|
93 | error_t hal_fpu_context_copy( thread_t * dst, |
---|
94 | thread_t * src ) |
---|
95 | { |
---|
96 | x86_panic((char *)__func__); |
---|
97 | return 0; |
---|
98 | } |
---|
99 | |
---|
100 | void hal_fpu_context_destroy( thread_t * thread ) |
---|
101 | { |
---|
102 | x86_panic((char *)__func__); |
---|
103 | } |
---|
104 | |
---|
105 | void hal_cpu_context_save( thread_t * thread ) |
---|
106 | { |
---|
107 | x86_panic((char *)__func__); |
---|
108 | } |
---|
109 | |
---|
110 | void hal_cpu_context_restore( thread_t * thread ) |
---|
111 | { |
---|
112 | x86_panic((char *)__func__); |
---|
113 | } |
---|
114 | |
---|
115 | void hal_cpu_context_load( thread_t * thread ) |
---|
116 | { |
---|
117 | x86_panic((char *)__func__); |
---|
118 | } |
---|
119 | |
---|
120 | void hal_fpu_context_save( thread_t * thread ) |
---|
121 | { |
---|
122 | x86_panic((char *)__func__); |
---|
123 | } |
---|
124 | |
---|
125 | void hal_fpu_context_restore( thread_t * thread ) |
---|
126 | { |
---|
127 | x86_panic((char *)__func__); |
---|
128 | } |
---|
129 | |
---|
130 | void hal_fpu_context_dup( xptr_t dst, |
---|
131 | xptr_t src ) |
---|
132 | { |
---|
133 | x86_panic((char *)__func__); |
---|
134 | } |
---|
135 | |
---|