]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/pc64/rebootcode.s
pc, pc64: properly track dependencies for mem.h on autogenerated apbootstrap.h and...
[plan9front.git] / sys / src / 9 / pc64 / rebootcode.s
1 #include "mem.h"
2
3 MODE $64
4
5 /*
6  * Turn off MMU, then memmove the new kernel to its correct location
7  * in physical memory.  Then jumps the to start of the kernel.
8  */
9
10 TEXT    main(SB), 1, $-4
11         MOVL    RARG, DI                /* destination */
12         MOVL    p2+8(FP), SI            /* source */
13         MOVL    n+16(FP), BX            /* byte count */
14
15         /* load zero length idt */
16         MOVL    $_idtptr64p<>(SB), AX
17         MOVL    (AX), IDTR
18
19         /* load temporary gdt */
20         MOVL    $_gdtptr64p<>(SB), AX
21         MOVL    (AX), GDTR
22
23         /* load CS with 32bit code segment */
24         PUSHQ   $SELECTOR(3, SELGDT, 0)
25         PUSHQ   $_warp32<>(SB)
26         RETFQ
27
28 MODE $32
29
30 TEXT    _warp32<>(SB), 1, $-4
31
32         /* load 32bit data segments */
33         MOVL    $SELECTOR(2, SELGDT, 0), AX
34         MOVW    AX, DS
35         MOVW    AX, ES
36         MOVW    AX, FS
37         MOVW    AX, GS
38         MOVW    AX, SS
39
40         /* turn off paging */
41         MOVL    CR0, AX
42         ANDL    $0x7fffffff, AX         /* ~(PG) */
43         MOVL    AX, CR0
44
45         MOVL    $0, AX
46         MOVL    AX, CR3
47
48         /* disable long mode */
49         MOVL    $0xc0000080, CX         /* Extended Feature Enable */
50         RDMSR
51         ANDL    $0xfffffeff, AX         /* Long Mode Disable */
52         WRMSR
53
54         /* diable pae */
55         MOVL    CR4, AX
56         ANDL    $0xffffff5f, AX         /* ~(PAE|PGE) */
57         MOVL    AX, CR4
58
59         MOVL    BX, CX                  /* byte count */
60         MOVL    DI, AX                  /* save entry point */
61         MOVL    AX, SP                  /* move stack below entry */
62
63         /* park cpu for zero entry point */
64         ORL     AX, AX
65         JZ      _idle
66
67
68 /*
69  * the source and destination may overlap.
70  * determine whether to copy forward or backwards
71  */
72         CMPL    SI, DI
73         JGT     _forward
74         MOVL    SI, DX
75         ADDL    CX, DX
76         CMPL    DX, DI
77         JGT     _back
78
79 _forward:
80         CLD
81         REP;    MOVSB
82
83 _startkernel:
84         /* jump to entry point */
85         JMP*    AX
86
87 _back:
88         ADDL    CX, DI
89         ADDL    CX, SI
90         SUBL    $1, DI
91         SUBL    $1, SI
92         STD
93         REP;    MOVSB
94         JMP     _startkernel
95
96 _idle:
97         HLT
98         JMP     _idle
99
100 TEXT _gdt<>(SB), 1, $-4
101         /* null descriptor */
102         LONG    $0
103         LONG    $0
104
105         /* (KESEG) 64 bit long mode exec segment */
106         LONG    $(0xFFFF)
107         LONG    $(SEGL|SEGG|SEGP|(0xF<<16)|SEGPL(0)|SEGEXEC|SEGR)
108
109         /* 32 bit data segment descriptor for 4 gigabytes (PL 0) */
110         LONG    $(0xFFFF)
111         LONG    $(SEGG|SEGB|(0xF<<16)|SEGP|SEGPL(0)|SEGDATA|SEGW)
112
113         /* 32 bit exec segment descriptor for 4 gigabytes (PL 0) */
114         LONG    $(0xFFFF)
115         LONG    $(SEGG|SEGD|(0xF<<16)|SEGP|SEGPL(0)|SEGEXEC|SEGR)
116
117 TEXT _gdtptr64p<>(SB), 1, $-4
118         WORD    $(4*8-1)
119         QUAD    $_gdt<>(SB)
120
121 TEXT _idtptr64p<>(SB), 1, $-4
122         WORD    $0
123         QUAD    $0