trap.c revision 1.4 1 1.3 nisimura /* $NetBSD: trap.c,v 1.4 2018/04/01 04:35:03 ryo Exp $ */
2 1.1 matt
3 1.1 matt /*-
4 1.1 matt * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.1 matt * by Matt Thomas of 3am Software Foundry.
9 1.1 matt *
10 1.1 matt * Redistribution and use in source and binary forms, with or without
11 1.1 matt * modification, are permitted provided that the following conditions
12 1.1 matt * are met:
13 1.1 matt * 1. Redistributions of source code must retain the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer.
15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 matt * notice, this list of conditions and the following disclaimer in the
17 1.1 matt * documentation and/or other materials provided with the distribution.
18 1.1 matt *
19 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
30 1.1 matt */
31 1.1 matt
32 1.1 matt #include <sys/cdefs.h>
33 1.1 matt
34 1.3 nisimura __KERNEL_RCSID(1, "$NetBSD: trap.c,v 1.4 2018/04/01 04:35:03 ryo Exp $");
35 1.1 matt
36 1.4 ryo #include "opt_arm_intr_impl.h"
37 1.4 ryo #include "opt_compat_netbsd32.h"
38 1.4 ryo
39 1.1 matt #include <sys/param.h>
40 1.1 matt #include <sys/types.h>
41 1.4 ryo #include <sys/atomic.h>
42 1.1 matt #include <sys/cpu.h>
43 1.4 ryo #ifdef KDB
44 1.4 ryo #include <sys/kdb.h>
45 1.4 ryo #endif
46 1.3 nisimura #include <sys/proc.h>
47 1.3 nisimura #include <sys/systm.h>
48 1.3 nisimura #include <sys/signal.h>
49 1.3 nisimura #include <sys/signalvar.h>
50 1.3 nisimura #include <sys/siginfo.h>
51 1.1 matt
52 1.4 ryo #ifdef ARM_INTR_IMPL
53 1.4 ryo #include ARM_INTR_IMPL
54 1.4 ryo #else
55 1.4 ryo #error ARM_INTR_IMPL not defined
56 1.4 ryo #endif
57 1.4 ryo
58 1.4 ryo #ifndef ARM_IRQ_HANDLER
59 1.4 ryo #error ARM_IRQ_HANDLER not defined
60 1.4 ryo #endif
61 1.4 ryo
62 1.4 ryo #include <aarch64/userret.h>
63 1.4 ryo #include <aarch64/frame.h>
64 1.4 ryo #include <aarch64/machdep.h>
65 1.4 ryo #include <aarch64/armreg.h>
66 1.1 matt #include <aarch64/locore.h>
67 1.1 matt
68 1.4 ryo #ifdef KDB
69 1.4 ryo #include <machine/db_machdep.h>
70 1.4 ryo #endif
71 1.4 ryo #ifdef DDB
72 1.4 ryo #include <ddb/db_output.h>
73 1.4 ryo #include <machine/db_machdep.h>
74 1.4 ryo #endif
75 1.4 ryo
76 1.4 ryo
77 1.4 ryo const char * const trap_names[] = {
78 1.4 ryo [ESR_EC_UNKNOWN] = "Unknown Reason (Illegal Instruction)",
79 1.4 ryo [ESR_EC_SERROR] = "SError Interrupt",
80 1.4 ryo [ESR_EC_WFX] = "WFI or WFE instruction execution",
81 1.4 ryo [ESR_EC_ILL_STATE] = "Illegal Execution State",
82 1.4 ryo
83 1.4 ryo [ESR_EC_SYS_REG] = "MSR/MRS/SYS instruction",
84 1.4 ryo [ESR_EC_SVC_A64] = "SVC Instruction Execution",
85 1.4 ryo [ESR_EC_HVC_A64] = "HVC Instruction Execution",
86 1.4 ryo [ESR_EC_SMC_A64] = "SMC Instruction Execution",
87 1.4 ryo
88 1.4 ryo [ESR_EC_INSN_ABT_EL0] = "Instruction Abort (EL0)",
89 1.4 ryo [ESR_EC_INSN_ABT_EL1] = "Instruction Abort (EL1)",
90 1.4 ryo [ESR_EC_DATA_ABT_EL0] = "Data Abort (EL0)",
91 1.4 ryo [ESR_EC_DATA_ABT_EL1] = "Data Abort (EL1)",
92 1.4 ryo
93 1.4 ryo [ESR_EC_PC_ALIGNMENT] = "Misaligned PC",
94 1.4 ryo [ESR_EC_SP_ALIGNMENT] = "Misaligned SP",
95 1.4 ryo
96 1.4 ryo [ESR_EC_FP_ACCESS] = "Access to SIMD/FP Registers",
97 1.4 ryo [ESR_EC_FP_TRAP_A64] = "FP Exception",
98 1.4 ryo
99 1.4 ryo [ESR_EC_BRKPNT_EL0] = "Breakpoint Exception (EL0)",
100 1.4 ryo [ESR_EC_BRKPNT_EL1] = "Breakpoint Exception (EL1)",
101 1.4 ryo [ESR_EC_SW_STEP_EL0] = "Software Step (EL0)",
102 1.4 ryo [ESR_EC_SW_STEP_EL1] = "Software Step (EL1)",
103 1.4 ryo [ESR_EC_WTCHPNT_EL0] = "Watchpoint (EL0)",
104 1.4 ryo [ESR_EC_WTCHPNT_EL1] = "Watchpoint (EL1)",
105 1.4 ryo [ESR_EC_BKPT_INSN_A64] = "BKPT Instruction Execution",
106 1.4 ryo
107 1.4 ryo [ESR_EC_CP15_RT] = "A32: MCR/MRC access to CP15",
108 1.4 ryo [ESR_EC_CP15_RRT] = "A32: MCRR/MRRC access to CP15",
109 1.4 ryo [ESR_EC_CP14_RT] = "A32: MCR/MRC access to CP14",
110 1.4 ryo [ESR_EC_CP14_DT] = "A32: LDC/STC access to CP14",
111 1.4 ryo [ESR_EC_CP14_RRT] = "A32: MRRC access to CP14",
112 1.4 ryo [ESR_EC_SVC_A32] = "A32: SVC Instruction Execution",
113 1.4 ryo [ESR_EC_HVC_A32] = "A32: HVC Instruction Execution",
114 1.4 ryo [ESR_EC_SMC_A32] = "A32: SMC Instruction Execution",
115 1.4 ryo [ESR_EC_FPID] = "A32: MCR/MRC access to CP10",
116 1.4 ryo [ESR_EC_FP_TRAP_A32] = "A32: FP Exception",
117 1.4 ryo [ESR_EC_BKPT_INSN_A32] = "A32: BKPT Instruction Execution",
118 1.4 ryo [ESR_EC_VECTOR_CATCH] = "A32: Vector Catch Exception"
119 1.4 ryo };
120 1.4 ryo
121 1.4 ryo static inline const char *
122 1.4 ryo eclass_trapname(uint32_t eclass)
123 1.3 nisimura {
124 1.4 ryo static char trapnamebuf[sizeof("Unknown trap 0x????????")];
125 1.4 ryo
126 1.4 ryo if (eclass >= __arraycount(trap_names) || trap_names[eclass] == NULL) {
127 1.4 ryo snprintf(trapnamebuf, sizeof(trapnamebuf),
128 1.4 ryo "Unknown trap 0x%02x", eclass);
129 1.4 ryo return trapnamebuf;
130 1.4 ryo }
131 1.4 ryo return trap_names[eclass];
132 1.3 nisimura }
133 1.3 nisimura
134 1.1 matt void
135 1.4 ryo userret(struct lwp *l)
136 1.1 matt {
137 1.1 matt mi_userret(l);
138 1.1 matt }
139 1.2 nisimura
140 1.3 nisimura void
141 1.4 ryo trap_doast(struct trapframe *tf)
142 1.3 nisimura {
143 1.3 nisimura struct lwp * const l = curlwp;
144 1.4 ryo
145 1.4 ryo /*
146 1.4 ryo * allow to have a chance of context switch just prior to user
147 1.4 ryo * exception return.
148 1.4 ryo */
149 1.4 ryo #ifdef __HAVE_PREEMPTION
150 1.4 ryo kpreempt_disable();
151 1.4 ryo #endif
152 1.4 ryo struct cpu_info * const ci = curcpu();
153 1.4 ryo
154 1.4 ryo ci->ci_data.cpu_ntrap++;
155 1.4 ryo
156 1.4 ryo KDASSERT(ci->ci_cpl == IPL_NONE);
157 1.4 ryo const int want_resched = ci->ci_want_resched;
158 1.4 ryo #ifdef __HAVE_PREEMPTION
159 1.4 ryo kpreempt_enable();
160 1.4 ryo #endif
161 1.4 ryo
162 1.4 ryo if (l->l_pflag & LP_OWEUPC) {
163 1.4 ryo l->l_pflag &= ~LP_OWEUPC;
164 1.4 ryo ADDUPROF(l);
165 1.3 nisimura }
166 1.4 ryo
167 1.4 ryo /* Allow a forced task switch. */
168 1.4 ryo if (want_resched)
169 1.4 ryo preempt();
170 1.4 ryo userret(l);
171 1.4 ryo }
172 1.4 ryo
173 1.4 ryo void
174 1.4 ryo trap_el1h_sync(struct trapframe *tf)
175 1.4 ryo {
176 1.4 ryo const uint32_t esr = tf->tf_esr;
177 1.4 ryo const uint32_t eclass = __SHIFTOUT(esr, ESR_EC); /* exception class */
178 1.4 ryo const char *trapname;
179 1.4 ryo
180 1.4 ryo /* re-enable traps and interrupts */
181 1.4 ryo if (!(tf->tf_spsr & SPSR_I))
182 1.4 ryo daif_enable(DAIF_D|DAIF_A|DAIF_I|DAIF_F);
183 1.4 ryo else
184 1.4 ryo daif_enable(DAIF_D|DAIF_A);
185 1.4 ryo
186 1.4 ryo trapname = eclass_trapname(eclass);
187 1.4 ryo
188 1.4 ryo switch (eclass) {
189 1.4 ryo case ESR_EC_INSN_ABT_EL1:
190 1.4 ryo case ESR_EC_DATA_ABT_EL1:
191 1.4 ryo data_abort_handler(tf, eclass, trapname);
192 1.4 ryo break;
193 1.4 ryo
194 1.4 ryo case ESR_EC_BRKPNT_EL1:
195 1.4 ryo case ESR_EC_SW_STEP_EL1:
196 1.4 ryo case ESR_EC_WTCHPNT_EL1:
197 1.4 ryo case ESR_EC_BKPT_INSN_A64:
198 1.4 ryo #ifdef DDB
199 1.4 ryo if (eclass == ESR_EC_BRKPNT_EL1)
200 1.4 ryo kdb_trap(DB_TRAP_BREAKPOINT, tf);
201 1.4 ryo else if (eclass == ESR_EC_BKPT_INSN_A64)
202 1.4 ryo kdb_trap(DB_TRAP_BKPT_INSN, tf);
203 1.4 ryo else if (eclass == ESR_EC_WTCHPNT_EL1)
204 1.4 ryo kdb_trap(DB_TRAP_WATCHPOINT, tf);
205 1.4 ryo else if (eclass == ESR_EC_SW_STEP_EL1)
206 1.4 ryo kdb_trap(DB_TRAP_SW_STEP, tf);
207 1.4 ryo else
208 1.4 ryo kdb_trap(DB_TRAP_UNKNOWN, tf);
209 1.4 ryo #else
210 1.4 ryo panic("No debugger in kernel");
211 1.4 ryo #endif
212 1.4 ryo break;
213 1.4 ryo
214 1.4 ryo case ESR_EC_FP_ACCESS:
215 1.4 ryo case ESR_EC_FP_TRAP_A64:
216 1.4 ryo case ESR_EC_PC_ALIGNMENT:
217 1.4 ryo case ESR_EC_SP_ALIGNMENT:
218 1.4 ryo case ESR_EC_ILL_STATE:
219 1.4 ryo default:
220 1.4 ryo panic("Trap: fatal %s: pc=%016llx sp=%016llx esr=%08x",
221 1.4 ryo trapname, tf->tf_pc, tf->tf_sp, esr);
222 1.4 ryo break;
223 1.3 nisimura }
224 1.3 nisimura }
225 1.3 nisimura
226 1.3 nisimura void
227 1.4 ryo trap_el0_sync(struct trapframe *tf)
228 1.3 nisimura {
229 1.4 ryo struct lwp * const l = curlwp;
230 1.4 ryo const uint32_t esr = tf->tf_esr;
231 1.4 ryo const uint32_t eclass = __SHIFTOUT(esr, ESR_EC); /* exception class */
232 1.4 ryo const char *trapname;
233 1.4 ryo
234 1.4 ryo /* enable traps and interrupts */
235 1.4 ryo daif_enable(DAIF_D|DAIF_A|DAIF_I|DAIF_F);
236 1.4 ryo
237 1.4 ryo trapname = eclass_trapname(eclass);
238 1.4 ryo
239 1.4 ryo switch (eclass) {
240 1.4 ryo case ESR_EC_INSN_ABT_EL0:
241 1.4 ryo case ESR_EC_DATA_ABT_EL0:
242 1.4 ryo data_abort_handler(tf, eclass, trapname);
243 1.4 ryo userret(l);
244 1.4 ryo break;
245 1.4 ryo
246 1.4 ryo case ESR_EC_SVC_A64:
247 1.4 ryo (*l->l_proc->p_md.md_syscall)(tf);
248 1.4 ryo break;
249 1.4 ryo case ESR_EC_FP_ACCESS:
250 1.4 ryo fpu_load(l);
251 1.4 ryo userret(l);
252 1.4 ryo break;
253 1.4 ryo case ESR_EC_FP_TRAP_A64:
254 1.4 ryo do_trapsignal(l, SIGFPE, FPE_FLTUND, NULL, esr); /* XXX */
255 1.4 ryo userret(l);
256 1.4 ryo break;
257 1.4 ryo
258 1.4 ryo case ESR_EC_PC_ALIGNMENT:
259 1.4 ryo do_trapsignal(l, SIGBUS, BUS_ADRALN, tf->tf_pc, esr);
260 1.4 ryo userret(l);
261 1.4 ryo break;
262 1.4 ryo case ESR_EC_SP_ALIGNMENT:
263 1.4 ryo do_trapsignal(l, SIGBUS, BUS_ADRALN, tf->tf_sp, esr);
264 1.4 ryo userret(l);
265 1.4 ryo break;
266 1.4 ryo
267 1.4 ryo case ESR_EC_BKPT_INSN_A64:
268 1.4 ryo case ESR_EC_BRKPNT_EL0:
269 1.4 ryo case ESR_EC_SW_STEP_EL0:
270 1.4 ryo case ESR_EC_WTCHPNT_EL0:
271 1.4 ryo /* XXX notyet */
272 1.4 ryo do_trapsignal(l, SIGTRAP, TRAP_BRKPT, tf->tf_pc, esr);
273 1.4 ryo userret(l);
274 1.4 ryo break;
275 1.4 ryo
276 1.4 ryo default:
277 1.4 ryo /* XXX notyet */
278 1.4 ryo case ESR_EC_UNKNOWN:
279 1.4 ryo /* illegal or not implemented instruction */
280 1.4 ryo do_trapsignal(l, SIGILL, ILL_ILLTRP, tf->tf_pc, esr);
281 1.4 ryo userret(l);
282 1.4 ryo break;
283 1.4 ryo }
284 1.3 nisimura }
285 1.3 nisimura
286 1.4 ryo void
287 1.4 ryo interrupt(struct trapframe *tf)
288 1.4 ryo {
289 1.4 ryo struct cpu_info * const ci = curcpu();
290 1.2 nisimura
291 1.4 ryo /* enable traps */
292 1.4 ryo daif_enable(DAIF_D|DAIF_A);
293 1.2 nisimura
294 1.4 ryo ci->ci_intr_depth++;
295 1.4 ryo ARM_IRQ_HANDLER(tf);
296 1.4 ryo ci->ci_intr_depth--;
297 1.2 nisimura
298 1.4 ryo cpu_dosoftints();
299 1.4 ryo }
300 1.2 nisimura
301 1.2 nisimura void
302 1.4 ryo trap_el0_32sync(struct trapframe *tf)
303 1.2 nisimura {
304 1.4 ryo struct lwp * const l = curlwp;
305 1.4 ryo const uint32_t esr = tf->tf_esr;
306 1.4 ryo const uint32_t eclass = __SHIFTOUT(esr, ESR_EC); /* exception class */
307 1.4 ryo const char *trapname;
308 1.4 ryo
309 1.4 ryo /* enable traps and interrupts */
310 1.4 ryo daif_enable(DAIF_D|DAIF_A|DAIF_I|DAIF_F);
311 1.4 ryo
312 1.4 ryo trapname = eclass_trapname(eclass);
313 1.4 ryo
314 1.4 ryo switch (eclass) {
315 1.4 ryo case ESR_EC_FP_ACCESS:
316 1.4 ryo fpu_load(l);
317 1.4 ryo userret(l);
318 1.4 ryo break;
319 1.4 ryo
320 1.4 ryo case ESR_EC_INSN_ABT_EL0:
321 1.4 ryo case ESR_EC_DATA_ABT_EL0:
322 1.4 ryo data_abort_handler(tf, eclass, trapname);
323 1.4 ryo userret(l);
324 1.4 ryo break;
325 1.4 ryo
326 1.4 ryo case ESR_EC_PC_ALIGNMENT:
327 1.4 ryo do_trapsignal(l, SIGBUS, BUS_ADRALN, tf->tf_pc, esr);
328 1.4 ryo userret(l);
329 1.4 ryo break;
330 1.4 ryo case ESR_EC_SP_ALIGNMENT:
331 1.4 ryo do_trapsignal(l, SIGBUS, BUS_ADRALN, tf->tf_sp, esr);
332 1.4 ryo userret(l);
333 1.4 ryo break;
334 1.4 ryo
335 1.4 ryo #ifdef COMPAT_NETBSD32
336 1.4 ryo case ESR_EC_SVC_A32:
337 1.4 ryo (*l->l_proc->p_md.md_syscall)(tf);
338 1.4 ryo break;
339 1.4 ryo case ESR_EC_CP15_RT:
340 1.4 ryo case ESR_EC_CP15_RRT:
341 1.4 ryo case ESR_EC_CP14_RT:
342 1.4 ryo case ESR_EC_CP14_DT:
343 1.4 ryo case ESR_EC_CP14_RRT:
344 1.4 ryo case ESR_EC_FP_TRAP_A32:
345 1.4 ryo case ESR_EC_BKPT_INSN_A32:
346 1.4 ryo /* XXX notyet */
347 1.4 ryo printf("%s:%d: %s\n", __func__, __LINE__, trapname);
348 1.4 ryo do_trapsignal(l, SIGILL, ILL_ILLTRP, tf->tf_pc, esr);
349 1.4 ryo userret(l);
350 1.4 ryo break;
351 1.4 ryo #endif /* COMPAT_NETBSD32 */
352 1.4 ryo default:
353 1.4 ryo /* XXX notyet */
354 1.4 ryo printf("%s:%d: %s\n", __func__, __LINE__, trapname);
355 1.4 ryo do_trapsignal(l, SIGILL, ILL_ILLTRP, tf->tf_pc, esr);
356 1.4 ryo userret(l);
357 1.4 ryo break;
358 1.4 ryo }
359 1.4 ryo }
360 1.4 ryo
361 1.4 ryo #define bad_trap_panic(trapfunc) \
362 1.4 ryo void \
363 1.4 ryo trapfunc(struct trapframe *tf) \
364 1.4 ryo { \
365 1.4 ryo panic("%s", __func__); \
366 1.4 ryo }
367 1.4 ryo bad_trap_panic(trap_el1t_sync)
368 1.4 ryo bad_trap_panic(trap_el1t_irq)
369 1.4 ryo bad_trap_panic(trap_el1t_fiq)
370 1.4 ryo bad_trap_panic(trap_el1t_error)
371 1.4 ryo bad_trap_panic(trap_el1h_fiq)
372 1.4 ryo bad_trap_panic(trap_el1h_error)
373 1.4 ryo bad_trap_panic(trap_el0_fiq)
374 1.4 ryo bad_trap_panic(trap_el0_error)
375 1.4 ryo bad_trap_panic(trap_el0_32fiq)
376 1.4 ryo bad_trap_panic(trap_el0_32error)
377 1.2 nisimura
378 1.4 ryo void
379 1.4 ryo cpu_jump_onfault(struct trapframe *tf, const struct faultbuf *fb, int val)
380 1.4 ryo {
381 1.2 nisimura tf->tf_reg[19] = fb->fb_reg[FB_X19];
382 1.2 nisimura tf->tf_reg[20] = fb->fb_reg[FB_X20];
383 1.2 nisimura tf->tf_reg[21] = fb->fb_reg[FB_X21];
384 1.2 nisimura tf->tf_reg[22] = fb->fb_reg[FB_X22];
385 1.2 nisimura tf->tf_reg[23] = fb->fb_reg[FB_X23];
386 1.2 nisimura tf->tf_reg[24] = fb->fb_reg[FB_X24];
387 1.2 nisimura tf->tf_reg[25] = fb->fb_reg[FB_X25];
388 1.2 nisimura tf->tf_reg[26] = fb->fb_reg[FB_X26];
389 1.2 nisimura tf->tf_reg[27] = fb->fb_reg[FB_X27];
390 1.2 nisimura tf->tf_reg[28] = fb->fb_reg[FB_X28];
391 1.2 nisimura tf->tf_reg[29] = fb->fb_reg[FB_X29];
392 1.2 nisimura tf->tf_sp = fb->fb_reg[FB_SP];
393 1.4 ryo tf->tf_pc = fb->fb_reg[FB_LR];
394 1.4 ryo tf->tf_reg[0] = val;
395 1.2 nisimura }
396 1.2 nisimura
397 1.2 nisimura void
398 1.4 ryo ucas_ras_check(struct trapframe *tf)
399 1.2 nisimura {
400 1.4 ryo #if 0 /* XXX notyet */
401 1.4 ryo extern char ucas_32_ras_start[];
402 1.4 ryo extern char ucas_32_ras_end[];
403 1.4 ryo extern char ucas_64_ras_start[];
404 1.4 ryo extern char ucas_64_ras_end[];
405 1.4 ryo
406 1.4 ryo if (tf->tf_pc > (vaddr_t)ucas_32_ras_start &&
407 1.4 ryo tf->tf_pc < (vaddr_t)ucas_32_ras_end) {
408 1.4 ryo tf->tf_pc = (vaddr_t)ucas_32_ras_start;
409 1.4 ryo } else if (tf->tf_pc > (vaddr_t)ucas_64_ras_start &&
410 1.4 ryo tf->tf_pc < (vaddr_t)ucas_64_ras_end) {
411 1.4 ryo tf->tf_pc = (vaddr_t)ucas_64_ras_start;
412 1.2 nisimura }
413 1.4 ryo #endif
414 1.2 nisimura }
415 1.2 nisimura
416 1.2 nisimura int
417 1.4 ryo kcopy(const void *src, void *dst, size_t len)
418 1.2 nisimura {
419 1.2 nisimura struct faultbuf fb;
420 1.2 nisimura int error;
421 1.2 nisimura
422 1.4 ryo if ((error = cpu_set_onfault(&fb)) == 0) {
423 1.4 ryo memcpy(dst, src, len);
424 1.2 nisimura cpu_unset_onfault();
425 1.2 nisimura }
426 1.2 nisimura return error;
427 1.2 nisimura }
428 1.2 nisimura
429 1.2 nisimura int
430 1.2 nisimura copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
431 1.2 nisimura {
432 1.2 nisimura struct faultbuf fb;
433 1.4 ryo size_t i;
434 1.2 nisimura int error;
435 1.4 ryo const char *src = kfaddr;
436 1.4 ryo char *dst = kdaddr;
437 1.2 nisimura
438 1.4 ryo if ((error = cpu_set_onfault(&fb)) == 0) {
439 1.4 ryo for (i = 0; i < len; i++) {
440 1.4 ryo if ((*dst++ = *src++) == '\0') {
441 1.4 ryo i++;
442 1.4 ryo error = 0;
443 1.4 ryo goto done;
444 1.4 ryo }
445 1.2 nisimura }
446 1.4 ryo error = ENAMETOOLONG;
447 1.4 ryo done:
448 1.4 ryo if (done != NULL)
449 1.4 ryo *done = i;
450 1.2 nisimura cpu_unset_onfault();
451 1.2 nisimura }
452 1.2 nisimura return error;
453 1.2 nisimura }
454