[29] | 1 | /* |
---|
| 2 | * hal_cpu.S - Miscellaneous CPU functions |
---|
[145] | 3 | * |
---|
[29] | 4 | * Copyright (c) 2017 Maxime Villard |
---|
[145] | 5 | * |
---|
[29] | 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 |
---|
[234] | 18 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
[29] | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #define x86_ASM |
---|
| 23 | #include <hal_boot.h> |
---|
[85] | 24 | #include <hal_segmentation.h> |
---|
[166] | 25 | #include <hal_register.h> |
---|
[29] | 26 | |
---|
| 27 | ASM_ENTRY(lgdt) |
---|
| 28 | lgdt (%rdi) |
---|
[85] | 29 | /* Reload the prefetch queue */ |
---|
[97] | 30 | jmp 1f |
---|
| 31 | nop |
---|
[85] | 32 | 1: /* Reload stale selectors */ |
---|
| 33 | movl $GDT_FIXED_SEL(GDT_KDATA_SEL, SEL_KPL),%eax |
---|
| 34 | movl %eax,%ds |
---|
| 35 | movl %eax,%es |
---|
| 36 | movl %eax,%ss |
---|
[29] | 37 | ret |
---|
| 38 | |
---|
| 39 | ASM_ENTRY(lidt) |
---|
| 40 | lidt (%rdi) |
---|
| 41 | ret |
---|
| 42 | |
---|
| 43 | ASM_ENTRY(ltr) |
---|
| 44 | ltr %di |
---|
| 45 | ret |
---|
| 46 | |
---|
[35] | 47 | ASM_ENTRY(invlpg) |
---|
| 48 | invlpg (%rdi) |
---|
| 49 | ret |
---|
| 50 | |
---|
[78] | 51 | ASM_ENTRY(sti) |
---|
| 52 | sti |
---|
| 53 | ret |
---|
| 54 | |
---|
| 55 | ASM_ENTRY(cli) |
---|
| 56 | cli |
---|
| 57 | ret |
---|
| 58 | |
---|
[94] | 59 | ASM_ENTRY(rdtsc) |
---|
[97] | 60 | xorq %rax,%rax |
---|
[94] | 61 | rdtsc |
---|
| 62 | shlq $32,%rdx |
---|
| 63 | orq %rdx,%rax |
---|
| 64 | ret |
---|
| 65 | |
---|
[91] | 66 | ASM_ENTRY(in8) |
---|
| 67 | movq %rdi,%rdx |
---|
| 68 | xorq %rax,%rax |
---|
| 69 | inb %dx,%al |
---|
| 70 | ret |
---|
| 71 | |
---|
[195] | 72 | ASM_ENTRY(in16) |
---|
| 73 | movq %rdi,%rdx |
---|
| 74 | xorq %rax,%rax |
---|
| 75 | inw %dx,%ax |
---|
| 76 | ret |
---|
| 77 | |
---|
[82] | 78 | ASM_ENTRY(out8) |
---|
| 79 | movq %rdi,%rdx |
---|
| 80 | movq %rsi,%rax |
---|
| 81 | outb %al,%dx |
---|
| 82 | ret |
---|
| 83 | |
---|
[195] | 84 | ASM_ENTRY(out16) |
---|
| 85 | movq %rdi,%rdx |
---|
| 86 | movq %rsi,%rax |
---|
| 87 | outw %ax,%dx |
---|
| 88 | ret |
---|
| 89 | |
---|
[46] | 90 | ASM_ENTRY(rdmsr) |
---|
| 91 | movq %rdi,%rcx |
---|
| 92 | xorq %rax,%rax |
---|
| 93 | rdmsr |
---|
| 94 | shlq $32,%rdx |
---|
| 95 | orq %rdx,%rax |
---|
| 96 | ret |
---|
| 97 | |
---|
| 98 | ASM_ENTRY(wrmsr) |
---|
| 99 | movq %rdi,%rcx |
---|
| 100 | movq %rsi,%rax |
---|
| 101 | movq %rsi,%rdx |
---|
| 102 | shrq $32,%rdx |
---|
| 103 | wrmsr |
---|
| 104 | ret |
---|
| 105 | |
---|
[125] | 106 | ASM_ENTRY(mfence) |
---|
| 107 | mfence |
---|
| 108 | ret |
---|
| 109 | |
---|
[236] | 110 | ASM_ENTRY(rcr0) |
---|
| 111 | movq %cr0,%rax |
---|
| 112 | ret |
---|
| 113 | |
---|
[99] | 114 | ASM_ENTRY(rcr2) |
---|
| 115 | movq %cr2,%rax |
---|
| 116 | ret |
---|
| 117 | |
---|
[236] | 118 | ASM_ENTRY(rcr3) |
---|
| 119 | movq %cr3,%rax |
---|
| 120 | ret |
---|
| 121 | |
---|
[166] | 122 | ASM_ENTRY(rcr4) |
---|
| 123 | movq %cr4,%rax |
---|
| 124 | ret |
---|
| 125 | |
---|
| 126 | ASM_ENTRY(lcr4) |
---|
| 127 | movq %rdi,%cr4 |
---|
| 128 | ret |
---|
| 129 | |
---|
| 130 | ASM_ENTRY(cpuid) |
---|
| 131 | movq %rbx,%r8 |
---|
| 132 | movq %rdi,%rax |
---|
| 133 | movq %rsi,%rcx |
---|
| 134 | movq %rdx,%rsi |
---|
| 135 | cpuid |
---|
| 136 | movl %eax,0(%rsi) |
---|
| 137 | movl %ebx,4(%rsi) |
---|
| 138 | movl %ecx,8(%rsi) |
---|
| 139 | movl %edx,12(%rsi) |
---|
| 140 | movq %r8,%rbx |
---|
| 141 | ret |
---|
| 142 | |
---|
| 143 | /* |
---|
| 144 | * To flush all TLB entries, we must re-set the CR4_PGE flag in %cr4. |
---|
| 145 | */ |
---|
| 146 | ASM_ENTRY(tlbflushg) |
---|
| 147 | movq %cr4,%rax |
---|
| 148 | movq %rax,%rdx |
---|
| 149 | andq $~CR4_PGE,%rdx |
---|
| 150 | movq %rdx,%cr4 |
---|
| 151 | movq %rax,%cr4 |
---|
| 152 | ret |
---|
| 153 | |
---|
| 154 | ASM_ENTRY(tlbflush) |
---|
| 155 | movq %cr3,%rax |
---|
| 156 | movq %rax,%cr3 |
---|
| 157 | ret |
---|
| 158 | |
---|
[82] | 159 | ASM_ENTRY(x86_stop) /* debug only */ |
---|
| 160 | int $0x0b |
---|
| 161 | ret |
---|
| 162 | |
---|
[235] | 163 | ASM_ENTRY(pause) |
---|
| 164 | pause |
---|
| 165 | ret |
---|
| 166 | |
---|
| 167 | ASM_ENTRY(wbinvd) |
---|
| 168 | wbinvd |
---|
| 169 | ret |
---|
| 170 | |
---|
[94] | 171 | /* -------------------------------------------------------------------------- */ |
---|
| 172 | |
---|
| 173 | ASM_ENTRY(atomic_cas_32) |
---|
| 174 | movl %esi,%eax |
---|
| 175 | lock |
---|
| 176 | cmpxchgl %edx,(%rdi) |
---|
| 177 | /* %eax now contains the old value */ |
---|
| 178 | ret |
---|
| 179 | |
---|
| 180 | ASM_ENTRY(atomic_add_32) |
---|
[97] | 181 | movl %esi,%eax |
---|
[94] | 182 | lock |
---|
[97] | 183 | xaddl %eax,(%rdi) |
---|
| 184 | /* %eax now contains the old value */ |
---|
[94] | 185 | ret |
---|
| 186 | |
---|
[98] | 187 | ASM_ENTRY(atomic_and_32) |
---|
| 188 | movl (%rdi),%eax |
---|
| 189 | 1: |
---|
| 190 | movl %eax,%ecx |
---|
| 191 | andl %esi,%ecx |
---|
| 192 | lock |
---|
| 193 | cmpxchgl %ecx,(%rdi) |
---|
| 194 | jnz 1b |
---|
| 195 | /* %eax now contains the old value */ |
---|
| 196 | ret |
---|
| 197 | |
---|
| 198 | ASM_ENTRY(atomic_or_32) |
---|
| 199 | movl (%rdi),%eax |
---|
| 200 | 1: |
---|
| 201 | movl %eax,%ecx |
---|
| 202 | orl %esi,%ecx |
---|
| 203 | lock |
---|
| 204 | cmpxchgl %ecx,(%rdi) |
---|
| 205 | jnz 1b |
---|
| 206 | /* %eax now contains the old value */ |
---|
| 207 | ret |
---|
| 208 | |
---|