| 1 | /* |
|---|
| 2 | * hal_internal.h - x86-specific function definitions |
|---|
| 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 <memcpy.h> |
|---|
| 23 | |
|---|
| 24 | #define offsetof(type, member) __builtin_offsetof(type, member) |
|---|
| 25 | #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) |
|---|
| 26 | #define rounddown(x,y) (((x)/(y))*(y)) |
|---|
| 27 | |
|---|
| 28 | #define XASSERT(a) \ |
|---|
| 29 | if (!(a)) { \ |
|---|
| 30 | x86_panic((char *)__func__); \ |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | /* hal_init.c */ |
|---|
| 34 | void cpu_activate(uint32_t gid); |
|---|
| 35 | int idt_slot_alloc( void ); |
|---|
| 36 | void idt_slot_free(int slot); |
|---|
| 37 | |
|---|
| 38 | /* hal_cpu.S */ |
|---|
| 39 | void invlpg(vaddr_t va); |
|---|
| 40 | void sti( void ); |
|---|
| 41 | void cli( void ); |
|---|
| 42 | uint64_t rdtsc( void ); |
|---|
| 43 | uint8_t in8(uint32_t port); |
|---|
| 44 | uint16_t in16(uint32_t port); |
|---|
| 45 | void out8(uint32_t port, uint8_t val); |
|---|
| 46 | void out16(uint32_t port, uint16_t val); |
|---|
| 47 | uint64_t rdmsr(uint32_t); |
|---|
| 48 | void wrmsr(uint32_t, uint64_t); |
|---|
| 49 | void mfence( void ); |
|---|
| 50 | uint64_t rcr0(void); |
|---|
| 51 | vaddr_t rcr2(void); |
|---|
| 52 | uint64_t rcr3(void); |
|---|
| 53 | void lcr3(uint64_t); |
|---|
| 54 | uint64_t rcr4(void); |
|---|
| 55 | void lcr4(uint64_t); |
|---|
| 56 | void cpuid(uint32_t eax, uint32_t ecx, uint32_t *desc); |
|---|
| 57 | void tlbflushg( void ); |
|---|
| 58 | void tlbflush( void ); |
|---|
| 59 | void clts( void ); |
|---|
| 60 | void stts( void ); |
|---|
| 61 | void pause( void ); |
|---|
| 62 | void wbinvd( void ); |
|---|
| 63 | |
|---|
| 64 | void cpu_context_switch(void *oldctx, void *newctx); |
|---|
| 65 | |
|---|
| 66 | uint32_t atomic_cas_32(volatile uint32_t *ptr, uint32_t exp, uint32_t new); |
|---|
| 67 | uint32_t atomic_add_32(volatile uint32_t *ptr, int32_t incr); |
|---|
| 68 | uint32_t atomic_and_32(volatile uint32_t *ptr, uint32_t mask); |
|---|
| 69 | uint32_t atomic_or_32(volatile uint32_t *ptr, uint32_t mask); |
|---|
| 70 | |
|---|
| 71 | /* hal_gpt.c */ |
|---|
| 72 | paddr_t hal_gpt_bootstrap_palloc(size_t npages); |
|---|
| 73 | vaddr_t hal_gpt_bootstrap_valloc(size_t npages); |
|---|
| 74 | void hal_gpt_bootstrap_reset( void ); |
|---|
| 75 | size_t hal_gpt_bootstrap_uniformize( void ); |
|---|
| 76 | |
|---|
| 77 | void hal_gpt_enter(vaddr_t va, paddr_t pa, pt_entry_t flags); |
|---|
| 78 | void hal_gpt_enter_range(vaddr_t va, paddr_t pa, size_t n); |
|---|
| 79 | void hal_gpt_leave(vaddr_t va); |
|---|
| 80 | void hal_gpt_leave_range(vaddr_t va, size_t n); |
|---|
| 81 | void hal_gpt_maptree_area(vaddr_t va_start, vaddr_t va_end); |
|---|
| 82 | void hal_gpt_init(paddr_t firstpa); |
|---|
| 83 | |
|---|
| 84 | /* hal_special.c */ |
|---|
| 85 | |
|---|
| 86 | /* x86_printf.c */ |
|---|
| 87 | void x86_lock( void ); |
|---|
| 88 | void x86_unlock( void ); |
|---|
| 89 | |
|---|
| 90 | void x86_panic(char *msg); |
|---|
| 91 | void x86_putc(char c); |
|---|
| 92 | void x86_printf(char *s, ...); |
|---|
| 93 | |
|---|