source: mainline/kernel/arch/arm64/src/arm64.c@ 14b5c30f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 14b5c30f was 14b5c30f, checked in by Martin Decky <martin@…>, 6 years ago

arm64: reset the kernel SP to its base value before entering user space

Without doing this, the part of the kernel stack that has been in use
before entering user space will remain forever unaccessible and wasted.

This is analogous to what is being done (by various means) on other
platforms.

  • Property mode set to 100644
File size: 6.6 KB
RevLine 
[84176f3]1/*
2 * Copyright (c) 2015 Petr Pavlu
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup kernel_arm64
30 * @{
31 */
32/** @file
33 * @brief ARM64 architecture specific functions.
34 */
35
36#include <abi/errno.h>
37#include <arch.h>
38#include <arch/arch.h>
39#include <arch/asm.h>
40#include <arch/exception.h>
41#include <arch/machine_func.h>
42#include <interrupt.h>
43#include <proc/scheduler.h>
44#include <syscall/syscall.h>
45#include <sysinfo/sysinfo.h>
46#include <userspace.h>
47
48static void arm64_post_mm_init(void);
49static void arm64_post_smp_init(void);
50
51arch_ops_t arm64_ops = {
52 .post_mm_init = arm64_post_mm_init,
53 .post_smp_init = arm64_post_smp_init,
54};
55
56arch_ops_t *arch_ops = &arm64_ops;
57
58/** Perform ARM64 specific initialization before main_bsp() is called. */
59void arm64_pre_main(void *entry __attribute__((unused)), bootinfo_t *bootinfo)
60{
61 /* Copy init task info. */
62 init.cnt = min3(bootinfo->taskmap.cnt, TASKMAP_MAX_RECORDS,
63 CONFIG_INIT_TASKS);
64
65 size_t i;
66 for (i = 0; i < init.cnt; i++) {
67 init.tasks[i].paddr =
68 (uintptr_t) bootinfo->taskmap.tasks[i].addr;
69 init.tasks[i].size = bootinfo->taskmap.tasks[i].size;
70 str_cpy(init.tasks[i].name, CONFIG_TASK_NAME_BUFLEN,
71 bootinfo->taskmap.tasks[i].name);
72 }
73
74 /* Copy physical memory map. */
75 memmap.cnt = min(bootinfo->memmap.cnt, MEMMAP_MAX_RECORDS);
76 for (i = 0; i < memmap.cnt; i++) {
77 memmap.zones[i].type = bootinfo->memmap.zones[i].type;
78 memmap.zones[i].start = bootinfo->memmap.zones[i].start;
79 memmap.zones[i].size = bootinfo->memmap.zones[i].size;
80 }
81
82 /* Initialize machine_ops pointer. */
83 machine_ops_init();
84}
85
86/** Perform ARM64 specific tasks needed before the memory management is
87 * initialized.
88 */
89void arm64_post_mm_init(void)
90{
91 if (config.cpu_active != 1)
92 return;
93
94 /* Do machine-specific initialization. */
95 machine_init();
96
97 /* Initialize exception dispatch table. */
98 exception_init();
99 interrupt_init();
100
101 /* Merge all memory zones to 1 big zone. */
102 zone_merge_all();
103
104 /* Initialize output device. */
105 machine_output_init();
106}
107
108/** Perform ARM64 specific tasks needed after the multiprocessing is
109 * initialized.
110 */
111void arm64_post_smp_init(void)
112{
113 /* Set platform name. */
114 const char *platform = machine_get_platform_name();
115
116 sysinfo_set_item_data("platform", NULL, (void *) platform,
117 str_size(platform));
118
119 /* Initialize input device. */
120 machine_input_init();
121}
122
123/** Calibrate delay loop.
124 *
125 * On ARM64, we implement delay() by waiting for the CNTVCT_EL0 register to
126 * reach a pre-computed value, as opposed to performing some pre-computed amount
127 * of instructions of known duration. We set the delay_loop_const to 1 in order
128 * to neutralize the multiplication done by delay().
129 */
130void calibrate_delay_loop(void)
131{
132 CPU->delay_loop_const = 1;
133}
134
135/** Wait several microseconds.
136 *
137 * @param t Microseconds to wait.
138 */
139void asm_delay_loop(uint32_t usec)
140{
141 uint64_t stop = CNTVCT_EL0_read() + usec * CNTFRQ_EL0_read() / 1000000;
142
143 while (CNTVCT_EL0_read() < stop)
144 ;
145}
146
147/** Change processor mode.
148 *
149 * @param kernel_uarg Userspace settings (entry point, stack, ...).
150 */
151void userspace(uspace_arg_t *kernel_uarg)
152{
153 /* Prepare return to EL0. */
154 SPSR_EL1_write((SPSR_EL1_read() & ~SPSR_MODE_MASK) |
155 SPSR_MODE_ARM64_EL0T);
156
157 /* Set program entry. */
[5a5269d]158 ELR_EL1_write(kernel_uarg->uspace_entry);
[84176f3]159
160 /* Set user stack. */
[5a5269d]161 SP_EL0_write(kernel_uarg->uspace_stack +
162 kernel_uarg->uspace_stack_size);
[84176f3]163
164 /* Clear Thread ID register. */
165 TPIDR_EL0_write(0);
166
167 asm volatile (
168 /*
[14b5c30f]169 * Reset the kernel stack to its base value.
170 *
171 * Clear all general-purpose registers,
172 * except x0 that holds an argument for
173 * the user space.
[84176f3]174 */
[14b5c30f]175 "mov sp, %[kstack]\n"
[84176f3]176 "mov x0, %[uspace_uarg]\n"
177 "mov x1, #0\n"
178 "mov x2, #0\n"
179 "mov x3, #0\n"
180 "mov x4, #0\n"
181 "mov x5, #0\n"
182 "mov x6, #0\n"
183 "mov x7, #0\n"
184 "mov x8, #0\n"
185 "mov x9, #0\n"
186 "mov x10, #0\n"
187 "mov x11, #0\n"
188 "mov x12, #0\n"
189 "mov x13, #0\n"
190 "mov x14, #0\n"
191 "mov x15, #0\n"
192 "mov x16, #0\n"
193 "mov x17, #0\n"
194 "mov x18, #0\n"
195 "mov x19, #0\n"
196 "mov x20, #0\n"
197 "mov x21, #0\n"
198 "mov x22, #0\n"
199 "mov x23, #0\n"
200 "mov x24, #0\n"
201 "mov x25, #0\n"
202 "mov x26, #0\n"
203 "mov x27, #0\n"
204 "mov x28, #0\n"
205 "mov x29, #0\n"
206 "mov x30, #0\n"
207 "eret\n"
[14b5c30f]208 :: [uspace_uarg] "r" (kernel_uarg->uspace_uarg),
209 [kstack] "r" (((uint64_t) (THREAD->kstack))
210 + MEM_STACK_SIZE - SP_DELTA)
[84176f3]211 );
212
213 unreachable();
214}
215
216/** Perform ARM64 specific tasks needed before the new task is run. */
217void before_task_runs_arch(void)
218{
219}
220
221/** Perform ARM64 specific tasks needed before the new thread is scheduled.
222 */
223void before_thread_runs_arch(void)
224{
225}
226
227/** Perform ARM64 specific tasks before a thread stops running. */
228void after_thread_ran_arch(void)
229{
230}
231
232/** Reboot the system. */
233void arch_reboot(void)
234{
235 /* Not implemented. */
236 while (true)
237 ;
238}
239
240/** Construct function pointer.
241 *
242 * @param fptr Function pointer structure.
243 * @param addr Function address.
244 * @param caller Calling function address.
245 *
246 * @return Address of the function pointer.
247 */
248void *arch_construct_function(fncptr_t *fptr, void *addr, void *caller)
249{
250 return addr;
251}
252
253/** Perform ARM64 specific tasks to initialize IRQ processing. */
254void irq_initialize_arch(irq_t *irq __attribute__((unused)))
255{
256}
257
258/** @}
259 */
Note: See TracBrowser for help on using the repository browser.