source: trunk/hal/x86_64/core/hal_cpu.S @ 340

Last change on this file since 340 was 339, checked in by max@…, 7 years ago

Add support for context switch - not tested yet, due to some other bugs in
the mapper. This cswitch is similar to that of TSAR.

File size: 4.2 KB
Line 
1/*
2 * hal_cpu.S - Miscellaneous CPU functions
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#define x86_ASM
23#include <hal_boot.h>
24#include <hal_segmentation.h>
25#include <hal_register.h>
26#include <hal_kentry.h>
27
28ASM_ENTRY(lgdt)
29        lgdt    (%rdi)
30        /* Reload the prefetch queue */
31        jmp     1f
32        nop
331:      /* Reload stale selectors */
34        movl    $GDT_FIXED_SEL(GDT_KDATA_SEL, SEL_KPL),%eax
35        movl    %eax,%ds
36        movl    %eax,%es
37        movl    %eax,%ss
38        ret
39
40ASM_ENTRY(lidt)
41        lidt    (%rdi)
42        ret
43
44ASM_ENTRY(ltr)
45        ltr     %di
46        ret
47
48ASM_ENTRY(invlpg)
49        invlpg  (%rdi)
50        ret
51
52ASM_ENTRY(sti)
53        sti
54        ret
55
56ASM_ENTRY(cli)
57        cli
58        ret
59
60ASM_ENTRY(rdtsc)
61        xorq    %rax,%rax
62        rdtsc
63        shlq    $32,%rdx
64        orq     %rdx,%rax
65        ret
66
67ASM_ENTRY(in8)
68        movq    %rdi,%rdx
69        xorq    %rax,%rax
70        inb     %dx,%al
71        ret
72
73ASM_ENTRY(in16)
74        movq    %rdi,%rdx
75        xorq    %rax,%rax
76        inw     %dx,%ax
77        ret
78
79ASM_ENTRY(out8)
80        movq    %rdi,%rdx
81        movq    %rsi,%rax
82        outb    %al,%dx
83        ret
84
85ASM_ENTRY(out16)
86        movq    %rdi,%rdx
87        movq    %rsi,%rax
88        outw    %ax,%dx
89        ret
90
91ASM_ENTRY(rdmsr)
92        movq    %rdi,%rcx
93        xorq    %rax,%rax
94        rdmsr
95        shlq    $32,%rdx
96        orq     %rdx,%rax
97        ret
98
99ASM_ENTRY(wrmsr)
100        movq    %rdi,%rcx
101        movq    %rsi,%rax
102        movq    %rsi,%rdx
103        shrq    $32,%rdx
104        wrmsr
105        ret
106
107ASM_ENTRY(mfence)
108        mfence
109        ret
110
111ASM_ENTRY(rcr0)
112        movq    %cr0,%rax
113        ret
114
115ASM_ENTRY(rcr2)
116        movq    %cr2,%rax
117        ret
118
119ASM_ENTRY(rcr3)
120        movq    %cr3,%rax
121        ret
122
123ASM_ENTRY(rcr4)
124        movq    %cr4,%rax
125        ret
126
127ASM_ENTRY(lcr4)
128        movq    %rdi,%cr4
129        ret
130
131ASM_ENTRY(cpuid)
132        movq    %rbx,%r8
133        movq    %rdi,%rax
134        movq    %rsi,%rcx
135        movq    %rdx,%rsi
136        cpuid
137        movl    %eax,0(%rsi)
138        movl    %ebx,4(%rsi)
139        movl    %ecx,8(%rsi)
140        movl    %edx,12(%rsi)
141        movq    %r8,%rbx
142        ret
143
144/*
145 * To flush all TLB entries, we must re-set the CR4_PGE flag in %cr4.
146 */
147ASM_ENTRY(tlbflushg)
148        movq    %cr4,%rax
149        movq    %rax,%rdx
150        andq    $~CR4_PGE,%rdx
151        movq    %rdx,%cr4
152        movq    %rax,%cr4
153        ret
154
155ASM_ENTRY(tlbflush)
156        movq    %cr3,%rax
157        movq    %rax,%cr3
158        ret
159
160ASM_ENTRY(clts)
161        clts
162        ret
163
164ASM_ENTRY(stts)
165        movq    %cr0,%rax
166        orq     $CR0_TS,%rax
167        movq    %rax,%cr0
168        ret
169
170ASM_ENTRY(x86_stop) /* debug only */
171        int     $0x0b
172        ret
173
174ASM_ENTRY(pause)
175        pause
176        ret
177
178ASM_ENTRY(wbinvd)
179        wbinvd
180        ret
181
182/* -------------------------------------------------------------------------- */
183
184ASM_ENTRY(cpu_context_switch)
185        pushq   %r12
186        pushq   %r13
187
188        movq    %rdi,%r13       /* oldctx */
189        movq    %rsi,%r12       /* newctx */
190
191        /*
192         * Save the current stack in %rdx, and switch to the trap frame of
193         * the old thread.
194         */
195        movq    %rsp,%rdx
196        movq    CTX_TF(%r13),%rsp
197        addq    $TF_SIZE,%rsp   /* end of the structure */
198
199        /* Build the trap frame */
200        movl    %ss,%eax
201        pushq   %rax            /* tf_ss */
202        pushq   %rdx            /* tf_rsp */
203        pushfq                  /* tf_rflags */
204        movl    %cs,%eax
205        pushq   %rax            /* tf_cs */
206        movabsq $thr_resume,%rax
207        pushq   %rax            /* tf_rip */
208        pushq   $0              /* tf_err */
209        pushq   $T_ASTFLT       /* tf_trapno */
210        INTR_SAVE_REGS
211
212        /* Switch rsp0 */
213        movq    CTX_RSP0(%r12),%rax
214        movq    TLSVAR(RSP0),%rdx
215        movq    %rax,(%rdx)
216
217        /* Switch to the new trap frame */
218        movq    CTX_TF(%r12),%rsp
219
220        /*
221         * Restore the context, and jump into the new thread.
222         */
223        INTR_RESTORE_REGS
224        iretq
225
226thr_resume:
227        /*
228         * Only pop %r12 and %r13, and return.
229         */
230        popq    %r13
231        popq    %r12
232
233        ret
234
235/* -------------------------------------------------------------------------- */
236
237ASM_ENTRY(atomic_cas_32)
238        movl    %esi,%eax
239        lock
240        cmpxchgl %edx,(%rdi)
241        /* %eax now contains the old value */
242        ret
243
244ASM_ENTRY(atomic_add_32)
245        movl    %esi,%eax
246        lock
247        xaddl   %eax,(%rdi)
248        /* %eax now contains the old value */
249        ret
250
251ASM_ENTRY(atomic_and_32)
252        movl    (%rdi),%eax
2531:
254        movl    %eax,%ecx
255        andl    %esi,%ecx
256        lock
257        cmpxchgl %ecx,(%rdi)
258        jnz     1b
259        /* %eax now contains the old value */
260        ret
261
262ASM_ENTRY(atomic_or_32)
263        movl    (%rdi),%eax
2641:
265        movl    %eax,%ecx
266        orl     %esi,%ecx
267        lock
268        cmpxchgl %ecx,(%rdi)
269        jnz     1b
270        /* %eax now contains the old value */
271        ret
272
Note: See TracBrowser for help on using the repository browser.