source: trunk/hal/x86_64/core/hal_context.c @ 359

Last change on this file since 359 was 359, checked in by max@…, 4 years ago

Synchronize tls_intr between context switches.

File size: 3.5 KB
Line 
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
37error_t hal_cpu_context_create(struct thread_s *thread)
38{
39        hal_trapframe_t *tf;
40        hal_cpu_context_t *ctx;
41        uint64_t kstacktop;
42        kmem_req_t req;
43
44        kstacktop = (uint64_t)thread->k_stack_base + thread->k_stack_size;
45
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
51        ctx = (hal_cpu_context_t *)kmem_alloc(&req);
52        if (ctx == NULL)
53                return ENOMEM;
54
55        /* set cpu context pointer in thread */
56        thread->cpu_context = (void *)ctx;
57
58        /*
59         * Build the context
60         */
61        ctx->ctx_rsp0 = kstacktop & ~0xF;
62        ctx->ctx_tf = (uint64_t)&ctx->ctx_hidden_tf;
63        ctx->ctx_intr = INTRS_ENABLED;
64
65        /*
66         * Build the trap frame
67         */
68        tf = &ctx->ctx_hidden_tf;
69        tf->tf_gs = GDT_FIXED_SEL(GDT_UDATA_SEL, SEL_UPL);
70        tf->tf_fs = GDT_FIXED_SEL(GDT_UDATA_SEL, SEL_UPL);
71        tf->tf_es = GDT_FIXED_SEL(GDT_UDATA_SEL, SEL_UPL);
72        tf->tf_ds = GDT_FIXED_SEL(GDT_UDATA_SEL, SEL_UPL);
73        tf->tf_rip = (uint64_t)thread->entry_func;
74        tf->tf_rflags = PSL_USERSET;
75
76        if (thread->type == THREAD_USER) {
77                tf->tf_cs = GDT_FIXED_SEL(GDT_UCODE_SEL, SEL_UPL);
78                tf->tf_ss = GDT_FIXED_SEL(GDT_UDATA_SEL, SEL_UPL);
79                tf->tf_rsp = ((uint64_t)thread->u_stack_base) +
80                    thread->u_stack_size;
81        } else {
82                tf->tf_cs = GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL);
83                tf->tf_ss = GDT_FIXED_SEL(GDT_KDATA_SEL, SEL_KPL);
84                tf->tf_rsp = kstacktop;
85        }
86
87        return 0;
88}
89
90error_t hal_cpu_context_copy(thread_t *dst, thread_t *src)
91{
92        x86_panic((char *)__func__);
93        return 0;
94}
95
96void hal_cpu_context_destroy(thread_t *thread)
97{
98        x86_panic((char *)__func__);
99}
100
101void hal_cpu_context_switch(thread_t *old, thread_t *new)
102{
103        curtls()->tls_thr = new;
104
105        /* Switch the VM space */
106        // TODO
107
108        /* Switch the CPU context */
109        cpu_context_switch(old->cpu_context, new->cpu_context);
110}
111
112void hal_cpu_context_load( thread_t * thread )
113{
114        x86_panic((char *)__func__);
115}
116
117error_t hal_fpu_context_create( thread_t * thread )
118{
119        x86_panic((char *)__func__);
120        return 0;
121}
122
123error_t hal_fpu_context_copy( thread_t * dst,
124                              thread_t * src )
125{
126        x86_panic((char *)__func__);
127        return 0;
128}
129
130void hal_fpu_context_destroy( thread_t * thread )
131{
132        x86_panic((char *)__func__);
133}
134
135void hal_fpu_context_save( thread_t * thread )
136{
137        x86_panic((char *)__func__);
138}
139
140void hal_fpu_context_restore( thread_t * thread )
141{
142        x86_panic((char *)__func__);
143}
144
145void hal_fpu_context_dup( xptr_t dst,
146                          xptr_t src )
147{
148        x86_panic((char *)__func__);
149}
150
Note: See TracBrowser for help on using the repository browser.