| 1 | /* |
|---|
| 2 | * hal_special.c - implementation of TLS 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 <hal_apic.h> |
|---|
| 24 | #include <hal_special.h> |
|---|
| 25 | #include <hal_register.h> |
|---|
| 26 | #include <hal_internal.h> |
|---|
| 27 | #include <hal_segmentation.h> |
|---|
| 28 | |
|---|
| 29 | #include <core.h> |
|---|
| 30 | #include <thread.h> |
|---|
| 31 | |
|---|
| 32 | struct thread_s; |
|---|
| 33 | |
|---|
| 34 | tls_t cpu0 __in_kdata; |
|---|
| 35 | |
|---|
| 36 | tls_t *curcpu() |
|---|
| 37 | { |
|---|
| 38 | tls_t *cputls; |
|---|
| 39 | |
|---|
| 40 | __asm volatile("movq %%gs:%1, %0" : |
|---|
| 41 | "=r" (cputls) : |
|---|
| 42 | "m" |
|---|
| 43 | (*(tls_t * const *)offsetof(tls_t, tls_self))); |
|---|
| 44 | return cputls; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | static void hal_tls_load_cpu(tls_t *cputls) |
|---|
| 48 | { |
|---|
| 49 | wrmsr(MSR_FSBASE, 0); |
|---|
| 50 | wrmsr(MSR_GSBASE, (uint64_t)cputls); |
|---|
| 51 | wrmsr(MSR_KERNELGSBASE, 0); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | void hal_tls_init_cpu0() |
|---|
| 55 | { |
|---|
| 56 | tls_t *cputls = &cpu0; |
|---|
| 57 | |
|---|
| 58 | memset(cputls, 0, sizeof(tls_t)); |
|---|
| 59 | |
|---|
| 60 | cputls->tls_self = cputls; |
|---|
| 61 | cputls->tls_gid = hal_lapic_gid(); |
|---|
| 62 | cputls->tls_lid = 0; /* XXX */ |
|---|
| 63 | |
|---|
| 64 | hal_tls_load_cpu(cputls); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | gid_t hal_get_gid() |
|---|
| 68 | { |
|---|
| 69 | return curcpu()->tls_gid; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | cycle_t hal_time_stamp() |
|---|
| 73 | { |
|---|
| 74 | return rdtsc(); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | uint64_t hal_get_cycles() |
|---|
| 78 | { |
|---|
| 79 | uint64_t cycles; |
|---|
| 80 | core_t *core = CURRENT_THREAD->core; |
|---|
| 81 | |
|---|
| 82 | /* |
|---|
| 83 | * Put the value of the TSC everywhere |
|---|
| 84 | */ |
|---|
| 85 | cycles = rdtsc(); |
|---|
| 86 | core->time_stamp = cycles; |
|---|
| 87 | core->cycles = cycles; |
|---|
| 88 | |
|---|
| 89 | return cycles; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | struct thread_s *hal_get_current_thread() |
|---|
| 93 | { |
|---|
| 94 | return curcpu()->tls_thr; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | void hal_set_current_thread( struct thread_s * thread ) |
|---|
| 98 | { |
|---|
| 99 | curcpu()->tls_thr = thread; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /* -------------------------------------------------------------------------- */ |
|---|
| 103 | |
|---|
| 104 | void hal_fpu_enable() |
|---|
| 105 | { |
|---|
| 106 | x86_panic((char *)__func__); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | void hal_fpu_disable() |
|---|
| 110 | { |
|---|
| 111 | x86_panic((char *)__func__); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | uint32_t hal_get_stack() |
|---|
| 115 | { |
|---|
| 116 | x86_panic((char *)__func__); |
|---|
| 117 | return 0; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | uint32_t hal_set_stack( void * new_val ) |
|---|
| 121 | { |
|---|
| 122 | x86_panic((char *)__func__); |
|---|
| 123 | return 0; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | uint32_t hal_get_bad_vaddr() |
|---|
| 127 | { |
|---|
| 128 | x86_panic((char *)__func__); |
|---|
| 129 | return 0; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | uint32_t hal_uncached_read( uint32_t * ptr ) |
|---|
| 133 | { |
|---|
| 134 | x86_panic((char *)__func__); |
|---|
| 135 | return 0; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | void hal_invalid_dcache_line( void * ptr ) |
|---|
| 139 | { |
|---|
| 140 | x86_panic((char *)__func__); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | void hal_fence() |
|---|
| 144 | { |
|---|
| 145 | mfence(); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | void hal_rdbar() |
|---|
| 149 | { |
|---|
| 150 | x86_panic((char *)__func__); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | void hal_core_sleep() |
|---|
| 154 | { |
|---|
| 155 | x86_panic((char *)__func__); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | void hal_fixed_delay( uint32_t delay ) |
|---|
| 159 | { |
|---|
| 160 | x86_panic((char *)__func__); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | void hal_get_mmu_excp( intptr_t * mmu_ins_excp_code, |
|---|
| 164 | intptr_t * mmu_ins_bad_vaddr, |
|---|
| 165 | intptr_t * mmu_dat_excp_code, |
|---|
| 166 | intptr_t * mmu_dat_bad_vaddr ) |
|---|
| 167 | { |
|---|
| 168 | x86_panic((char *)__func__); |
|---|
| 169 | } |
|---|