source: mainline/kernel/arch/arm64/src/arm64.c@ 9532981

Last change on this file since 9532981 was 5a5269d, checked in by GitHub <noreply@…>, 6 years ago

Change type of uspace pointers in kernel from pointer type to numeric (#170)

From kernel's perspective, userspace addresses are not valid pointers,
and can only be used in calls to copy_to/from_uspace().
Therefore, we change the type of those arguments and variables to
uspace_addr_t which is an alias for sysarg_t.

This allows the compiler to catch accidental direct accesses to
userspace addresses.

Additionally, to avoid losing the type information in code,
a macro uspace_ptr(type) is used that translates to uspace_addr_t.
I makes no functional difference, but allows keeping the type information
in code in case we implement some sort of static checking for it in the future.

However, ccheck doesn't like that, so instead of using uspace_ptr(char),
we use uspace_ptr_char which is defined as
#define uspace_ptr_char uspace_ptr(char).

  • Property mode set to 100644
File size: 6.5 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 /*
169 * Clear all general-purpose registers, except x0 that holds an
170 * argument for the user space.
171 */
172 "mov x0, %[uspace_uarg]\n"
173 "mov x1, #0\n"
174 "mov x2, #0\n"
175 "mov x3, #0\n"
176 "mov x4, #0\n"
177 "mov x5, #0\n"
178 "mov x6, #0\n"
179 "mov x7, #0\n"
180 "mov x8, #0\n"
181 "mov x9, #0\n"
182 "mov x10, #0\n"
183 "mov x11, #0\n"
184 "mov x12, #0\n"
185 "mov x13, #0\n"
186 "mov x14, #0\n"
187 "mov x15, #0\n"
188 "mov x16, #0\n"
189 "mov x17, #0\n"
190 "mov x18, #0\n"
191 "mov x19, #0\n"
192 "mov x20, #0\n"
193 "mov x21, #0\n"
194 "mov x22, #0\n"
195 "mov x23, #0\n"
196 "mov x24, #0\n"
197 "mov x25, #0\n"
198 "mov x26, #0\n"
199 "mov x27, #0\n"
200 "mov x28, #0\n"
201 "mov x29, #0\n"
202 "mov x30, #0\n"
203 "eret\n"
204 :: [uspace_uarg] "r" (kernel_uarg->uspace_uarg)
205 );
206
207 unreachable();
208}
209
210/** Perform ARM64 specific tasks needed before the new task is run. */
211void before_task_runs_arch(void)
212{
213}
214
215/** Perform ARM64 specific tasks needed before the new thread is scheduled.
216 */
217void before_thread_runs_arch(void)
218{
219}
220
221/** Perform ARM64 specific tasks before a thread stops running. */
222void after_thread_ran_arch(void)
223{
224}
225
226/** Reboot the system. */
227void arch_reboot(void)
228{
229 /* Not implemented. */
230 while (true)
231 ;
232}
233
234/** Construct function pointer.
235 *
236 * @param fptr Function pointer structure.
237 * @param addr Function address.
238 * @param caller Calling function address.
239 *
240 * @return Address of the function pointer.
241 */
242void *arch_construct_function(fncptr_t *fptr, void *addr, void *caller)
243{
244 return addr;
245}
246
247/** Perform ARM64 specific tasks to initialize IRQ processing. */
248void irq_initialize_arch(irq_t *irq __attribute__((unused)))
249{
250}
251
252/** @}
253 */
Note: See TracBrowser for help on using the repository browser.