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

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

Mmh, skip tf_trapno and tf_err for iretq.

File size: 4.2 KB
RevLine 
[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>
[339]26#include <hal_kentry.h>
[29]27
28ASM_ENTRY(lgdt)
29        lgdt    (%rdi)
[85]30        /* Reload the prefetch queue */
[97]31        jmp     1f
32        nop
[85]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
[29]38        ret
39
40ASM_ENTRY(lidt)
41        lidt    (%rdi)
42        ret
43
44ASM_ENTRY(ltr)
45        ltr     %di
46        ret
47
[35]48ASM_ENTRY(invlpg)
49        invlpg  (%rdi)
50        ret
51
[78]52ASM_ENTRY(sti)
53        sti
54        ret
55
56ASM_ENTRY(cli)
57        cli
58        ret
59
[94]60ASM_ENTRY(rdtsc)
[97]61        xorq    %rax,%rax
[94]62        rdtsc
63        shlq    $32,%rdx
64        orq     %rdx,%rax
65        ret
66
[91]67ASM_ENTRY(in8)
68        movq    %rdi,%rdx
69        xorq    %rax,%rax
70        inb     %dx,%al
71        ret
72
[195]73ASM_ENTRY(in16)
74        movq    %rdi,%rdx
75        xorq    %rax,%rax
76        inw     %dx,%ax
77        ret
78
[82]79ASM_ENTRY(out8)
80        movq    %rdi,%rdx
81        movq    %rsi,%rax
82        outb    %al,%dx
83        ret
84
[195]85ASM_ENTRY(out16)
86        movq    %rdi,%rdx
87        movq    %rsi,%rax
88        outw    %ax,%dx
89        ret
90
[46]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
[125]107ASM_ENTRY(mfence)
108        mfence
109        ret
110
[236]111ASM_ENTRY(rcr0)
112        movq    %cr0,%rax
113        ret
114
[99]115ASM_ENTRY(rcr2)
116        movq    %cr2,%rax
117        ret
118
[236]119ASM_ENTRY(rcr3)
120        movq    %cr3,%rax
121        ret
122
[166]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
[309]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
[82]170ASM_ENTRY(x86_stop) /* debug only */
171        int     $0x0b
172        ret
173
[235]174ASM_ENTRY(pause)
175        pause
176        ret
177
178ASM_ENTRY(wbinvd)
179        wbinvd
180        ret
181
[94]182/* -------------------------------------------------------------------------- */
183
[339]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
[343]224        addq    $16,%rsp
[339]225        iretq
226
227thr_resume:
228        /*
229         * Only pop %r12 and %r13, and return.
230         */
231        popq    %r13
232        popq    %r12
233
234        ret
235
236/* -------------------------------------------------------------------------- */
237
[94]238ASM_ENTRY(atomic_cas_32)
239        movl    %esi,%eax
240        lock
241        cmpxchgl %edx,(%rdi)
242        /* %eax now contains the old value */
243        ret
244
245ASM_ENTRY(atomic_add_32)
[97]246        movl    %esi,%eax
[94]247        lock
[97]248        xaddl   %eax,(%rdi)
249        /* %eax now contains the old value */
[94]250        ret
251
[98]252ASM_ENTRY(atomic_and_32)
253        movl    (%rdi),%eax
2541:
255        movl    %eax,%ecx
256        andl    %esi,%ecx
257        lock
258        cmpxchgl %ecx,(%rdi)
259        jnz     1b
260        /* %eax now contains the old value */
261        ret
262
263ASM_ENTRY(atomic_or_32)
264        movl    (%rdi),%eax
2651:
266        movl    %eax,%ecx
267        orl     %esi,%ecx
268        lock
269        cmpxchgl %ecx,(%rdi)
270        jnz     1b
271        /* %eax now contains the old value */
272        ret
273
Note: See TracBrowser for help on using the repository browser.