x86: relocate_kernel - use PAGE_SIZE instead of numeric constant
[linux-2.6/mini2440.git] / arch / x86 / kernel / relocate_kernel_64.S
blobc2c8b9d6e241ff0969ffb1eaa99b903d63223e4d
1 /*
2  * relocate_kernel.S - put the kernel image in place to boot
3  * Copyright (C) 2002-2005 Eric Biederman  <ebiederm@xmission.com>
4  *
5  * This source code is licensed under the GNU General Public License,
6  * Version 2.  See the file COPYING for more details.
7  */
9 #include <linux/linkage.h>
10 #include <asm/page.h>
11 #include <asm/kexec.h>
14  * Must be relocatable PIC code callable as a C function
15  */
17 #define PTR(x) (x << 3)
18 #define PAGE_ATTR 0x63 /* _PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY */
20         .text
21         .align PAGE_SIZE
22         .code64
23         .globl relocate_kernel
24 relocate_kernel:
25         /* %rdi indirection_page
26          * %rsi page_list
27          * %rdx start address
28          */
30         /* map the control page at its virtual address */
32         movq    $0x0000ff8000000000, %r10        /* mask */
33         mov     $(39 - 3), %cl                   /* bits to shift */
34         movq    PTR(VA_CONTROL_PAGE)(%rsi), %r11 /* address to map */
36         movq    %r11, %r9
37         andq    %r10, %r9
38         shrq    %cl, %r9
40         movq    PTR(VA_PGD)(%rsi), %r8
41         addq    %r8, %r9
42         movq    PTR(PA_PUD_0)(%rsi), %r8
43         orq     $PAGE_ATTR, %r8
44         movq    %r8, (%r9)
46         shrq    $9, %r10
47         sub     $9, %cl
49         movq    %r11, %r9
50         andq    %r10, %r9
51         shrq    %cl, %r9
53         movq    PTR(VA_PUD_0)(%rsi), %r8
54         addq    %r8, %r9
55         movq    PTR(PA_PMD_0)(%rsi), %r8
56         orq     $PAGE_ATTR, %r8
57         movq    %r8, (%r9)
59         shrq    $9, %r10
60         sub     $9, %cl
62         movq    %r11, %r9
63         andq    %r10, %r9
64         shrq    %cl, %r9
66         movq    PTR(VA_PMD_0)(%rsi), %r8
67         addq    %r8, %r9
68         movq    PTR(PA_PTE_0)(%rsi), %r8
69         orq     $PAGE_ATTR, %r8
70         movq    %r8, (%r9)
72         shrq    $9, %r10
73         sub     $9, %cl
75         movq    %r11, %r9
76         andq    %r10, %r9
77         shrq    %cl, %r9
79         movq    PTR(VA_PTE_0)(%rsi), %r8
80         addq    %r8, %r9
81         movq    PTR(PA_CONTROL_PAGE)(%rsi), %r8
82         orq     $PAGE_ATTR, %r8
83         movq    %r8, (%r9)
85         /* identity map the control page at its physical address */
87         movq    $0x0000ff8000000000, %r10        /* mask */
88         mov     $(39 - 3), %cl                   /* bits to shift */
89         movq    PTR(PA_CONTROL_PAGE)(%rsi), %r11 /* address to map */
91         movq    %r11, %r9
92         andq    %r10, %r9
93         shrq    %cl, %r9
95         movq    PTR(VA_PGD)(%rsi), %r8
96         addq    %r8, %r9
97         movq    PTR(PA_PUD_1)(%rsi), %r8
98         orq     $PAGE_ATTR, %r8
99         movq    %r8, (%r9)
101         shrq    $9, %r10
102         sub     $9, %cl
104         movq    %r11, %r9
105         andq    %r10, %r9
106         shrq    %cl, %r9
108         movq    PTR(VA_PUD_1)(%rsi), %r8
109         addq    %r8, %r9
110         movq    PTR(PA_PMD_1)(%rsi), %r8
111         orq     $PAGE_ATTR, %r8
112         movq    %r8, (%r9)
114         shrq    $9, %r10
115         sub     $9, %cl
117         movq    %r11, %r9
118         andq    %r10, %r9
119         shrq    %cl, %r9
121         movq    PTR(VA_PMD_1)(%rsi), %r8
122         addq    %r8, %r9
123         movq    PTR(PA_PTE_1)(%rsi), %r8
124         orq     $PAGE_ATTR, %r8
125         movq    %r8, (%r9)
127         shrq    $9, %r10
128         sub     $9, %cl
130         movq    %r11, %r9
131         andq    %r10, %r9
132         shrq    %cl, %r9
134         movq    PTR(VA_PTE_1)(%rsi), %r8
135         addq    %r8, %r9
136         movq    PTR(PA_CONTROL_PAGE)(%rsi), %r8
137         orq     $PAGE_ATTR, %r8
138         movq    %r8, (%r9)
140 relocate_new_kernel:
141         /* %rdi indirection_page
142          * %rsi page_list
143          * %rdx start address
144          */
146         /* zero out flags, and disable interrupts */
147         pushq $0
148         popfq
150         /* get physical address of control page now */
151         /* this is impossible after page table switch */
152         movq    PTR(PA_CONTROL_PAGE)(%rsi), %r8
154         /* get physical address of page table now too */
155         movq    PTR(PA_TABLE_PAGE)(%rsi), %rcx
157         /* switch to new set of page tables */
158         movq    PTR(PA_PGD)(%rsi), %r9
159         movq    %r9, %cr3
161         /* setup a new stack at the end of the physical control page */
162         lea     PAGE_SIZE(%r8), %rsp
164         /* jump to identity mapped page */
165         addq    $(identity_mapped - relocate_kernel), %r8
166         pushq   %r8
167         ret
169 identity_mapped:
170         /* store the start address on the stack */
171         pushq   %rdx
173         /* Set cr0 to a known state:
174          * 31 1 == Paging enabled
175          * 18 0 == Alignment check disabled
176          * 16 0 == Write protect disabled
177          * 3  0 == No task switch
178          * 2  0 == Don't do FP software emulation.
179          * 0  1 == Proctected mode enabled
180          */
181         movq    %cr0, %rax
182         andq    $~((1<<18)|(1<<16)|(1<<3)|(1<<2)), %rax
183         orl     $((1<<31)|(1<<0)), %eax
184         movq    %rax, %cr0
186         /* Set cr4 to a known state:
187          * 10 0 == xmm exceptions disabled
188          * 9  0 == xmm registers instructions disabled
189          * 8  0 == performance monitoring counter disabled
190          * 7  0 == page global disabled
191          * 6  0 == machine check exceptions disabled
192          * 5  1 == physical address extension enabled
193          * 4  0 == page size extensions disabled
194          * 3  0 == Debug extensions disabled
195          * 2  0 == Time stamp disable (disabled)
196          * 1  0 == Protected mode virtual interrupts disabled
197          * 0  0 == VME disabled
198          */
200         movq    $((1<<5)), %rax
201         movq    %rax, %cr4
203         jmp 1f
206         /* Switch to the identity mapped page tables,
207          * and flush the TLB.
208         */
209         movq    %rcx, %cr3
211         /* Do the copies */
212         movq    %rdi, %rcx      /* Put the page_list in %rcx */
213         xorq    %rdi, %rdi
214         xorq    %rsi, %rsi
215         jmp     1f
217 0:      /* top, read another word for the indirection page */
219         movq    (%rbx), %rcx
220         addq    $8,     %rbx
222         testq   $0x1,   %rcx  /* is it a destination page? */
223         jz      2f
224         movq    %rcx,   %rdi
225         andq    $0xfffffffffffff000, %rdi
226         jmp     0b
228         testq   $0x2,   %rcx  /* is it an indirection page? */
229         jz      2f
230         movq    %rcx,   %rbx
231         andq    $0xfffffffffffff000, %rbx
232         jmp     0b
234         testq   $0x4,   %rcx  /* is it the done indicator? */
235         jz      2f
236         jmp     3f
238         testq   $0x8,   %rcx  /* is it the source indicator? */
239         jz      0b            /* Ignore it otherwise */
240         movq    %rcx,   %rsi  /* For ever source page do a copy */
241         andq    $0xfffffffffffff000, %rsi
243         movq    $512,   %rcx
244         rep ; movsq
245         jmp     0b
248         /* To be certain of avoiding problems with self-modifying code
249          * I need to execute a serializing instruction here.
250          * So I flush the TLB by reloading %cr3 here, it's handy,
251          * and not processor dependent.
252          */
253         movq    %cr3, %rax
254         movq    %rax, %cr3
256         /* set all of the registers to known values */
257         /* leave %rsp alone */
259         xorq    %rax, %rax
260         xorq    %rbx, %rbx
261         xorq    %rcx, %rcx
262         xorq    %rdx, %rdx
263         xorq    %rsi, %rsi
264         xorq    %rdi, %rdi
265         xorq    %rbp, %rbp
266         xorq    %r8,  %r8
267         xorq    %r9,  %r9
268         xorq    %r10, %r9
269         xorq    %r11, %r11
270         xorq    %r12, %r12
271         xorq    %r13, %r13
272         xorq    %r14, %r14
273         xorq    %r15, %r15
275         ret