machdep.c 39.1 KB
Newer Older
cognet's avatar
cognet committed
1
2
/*	$NetBSD: arm32_machdep.c,v 1.44 2004/03/24 15:34:47 atatat Exp $	*/

imp's avatar
imp committed
3
/*-
cognet's avatar
cognet committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 * Copyright (c) 2004 Olivier Houchard
 * Copyright (c) 1994-1998 Mark Brinicombe.
 * Copyright (c) 1994 Brini.
 * All rights reserved.
 *
 * This code is derived from software written for Brini by Mark Brinicombe
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Mark Brinicombe
 *	for the NetBSD Project.
 * 4. The name of the company nor the name of the author may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Machine dependant functions for kernel setup
 *
 * Created      : 17/09/94
 * Updated	: 18/04/01 updated for new wscons
 */

#include "opt_compat.h"
46
#include "opt_ddb.h"
47
#include "opt_pax.h"
andrew's avatar
andrew committed
48
#include "opt_platform.h"
49
#include "opt_sched.h"
gonzo's avatar
gonzo committed
50
#include "opt_timer.h"
51

cognet's avatar
cognet committed
52
53
54
55
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
56
#include <sys/proc.h>
cognet's avatar
cognet committed
57
#include <sys/systm.h>
58
59
60
#include <sys/bio.h>
#include <sys/buf.h>
#include <sys/bus.h>
61
#include <sys/cons.h>
62
#include <sys/cpu.h>
63
#include <sys/exec.h>
cognet's avatar
cognet committed
64
#include <sys/imgact.h>
andrew's avatar
andrew committed
65
#include <sys/kdb.h>
cognet's avatar
cognet committed
66
#include <sys/kernel.h>
kevlo's avatar
kevlo committed
67
#include <sys/ktr.h>
cognet's avatar
cognet committed
68
69
70
#include <sys/linker.h>
#include <sys/lock.h>
#include <sys/malloc.h>
andrew's avatar
andrew committed
71
#include <sys/msgbuf.h>
cognet's avatar
cognet committed
72
#include <sys/mutex.h>
73
#include <sys/pax.h>
cognet's avatar
cognet committed
74
75
#include <sys/pcpu.h>
#include <sys/ptrace.h>
attilio's avatar
attilio committed
76
#include <sys/rwlock.h>
77
#include <sys/sched.h>
78
#include <sys/signalvar.h>
79
#include <sys/syscallsubr.h>
80
#include <sys/sysctl.h>
cognet's avatar
cognet committed
81
#include <sys/sysent.h>
82
#include <sys/sysproto.h>
83
#include <sys/uio.h>
cognet's avatar
cognet committed
84
85

#include <vm/vm.h>
86
#include <vm/pmap.h>
87
#include <vm/vm_map.h>
cognet's avatar
cognet committed
88
89
90
#include <vm/vm_object.h>
#include <vm/vm_page.h>
#include <vm/vm_pager.h>
91

cognet's avatar
cognet committed
92
#include <machine/armreg.h>
93
#include <machine/atags.h>
94
#include <machine/cpu.h>
95
#include <machine/cpuinfo.h>
96
#include <machine/devmap.h>
97
#include <machine/frame.h>
98
#include <machine/intr.h>
99
#include <machine/machdep.h>
cognet's avatar
cognet committed
100
#include <machine/md_var.h>
101
102
#include <machine/metadata.h>
#include <machine/pcb.h>
103
#include <machine/physmem.h>
104
#include <machine/platform.h>
105
106
107
#include <machine/reg.h>
#include <machine/trap.h>
#include <machine/undefined.h>
108
#include <machine/vfp.h>
109
#include <machine/vmparam.h>
110
#include <machine/sysarch.h>
cognet's avatar
cognet committed
111

andrew's avatar
andrew committed
112
113
114
115
116
#ifdef FDT
#include <dev/fdt/fdt_common.h>
#include <dev/ofw/openfirm.h>
#endif

117
118
119
120
#ifdef DDB
#include <ddb/ddb.h>
#endif

andrew's avatar
andrew committed
121
122
123
124
125
126
#ifdef DEBUG
#define	debugf(fmt, args...) printf(fmt, ##args)
#else
#define	debugf(fmt, args...)
#endif

gonzo's avatar
gonzo committed
127
128
struct pcpu __pcpu[MAXCPU];
struct pcpu *pcpup = &__pcpu[0];
129

gonzo's avatar
gonzo committed
130
static struct trapframe proc0_tf;
cognet's avatar
cognet committed
131
132
133
134
uint32_t cpu_reset_address = 0;
int cold = 1;
vm_offset_t vector_page;

135
136
137
138
139
int (*_arm_memcpy)(void *, void *, int, int) = NULL;
int (*_arm_bzero)(void *, int, int) = NULL;
int _min_memcpy_size = 0;
int _min_bzero_size = 0;

140
141
extern int *end;

andrew's avatar
andrew committed
142
#ifdef FDT
143
144
145
146
147
148
149
150
vm_paddr_t pmap_pa;

#ifdef ARM_NEW_PMAP
vm_offset_t systempage;
vm_offset_t irqstack;
vm_offset_t undstack;
vm_offset_t abtstack;
#else
andrew's avatar
andrew committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * This is the number of L2 page tables required for covering max
 * (hypothetical) memsize of 4GB and all kernel mappings (vectors, msgbuf,
 * stacks etc.), uprounded to be divisible by 4.
 */
#define KERNEL_PT_MAX	78

static struct pv_addr kernel_pt_table[KERNEL_PT_MAX];

struct pv_addr systempage;
static struct pv_addr msgbufpv;
struct pv_addr irqstack;
struct pv_addr undstack;
struct pv_addr abtstack;
static struct pv_addr kernelstack;
166
#endif
andrew's avatar
andrew committed
167
168
#endif

169
170
171
172
173
174
175
176
177
178
179
180
#if defined(LINUX_BOOT_ABI)
#define LBABI_MAX_BANKS	10

uint32_t board_id;
struct arm_lbabi_tag *atag_list;
char linux_command_line[LBABI_MAX_COMMAND_LINE + 1];
char atags[LBABI_MAX_COMMAND_LINE * 2];
uint32_t memstart[LBABI_MAX_BANKS];
uint32_t memsize[LBABI_MAX_BANKS];
uint32_t membanks;
#endif

181
182
183
184
185
186
187
188
189
190
static uint32_t board_revision;
/* hex representation of uint64_t */
static char board_serial[32];

SYSCTL_NODE(_hw, OID_AUTO, board, CTLFLAG_RD, 0, "Board attributes");
SYSCTL_UINT(_hw_board, OID_AUTO, revision, CTLFLAG_RD,
    &board_revision, 0, "Board revision");
SYSCTL_STRING(_hw_board, OID_AUTO, serial, CTLFLAG_RD,
    board_serial, 0, "Board serial");

191
192
193
194
int vfp_exists;
SYSCTL_INT(_hw, HW_FLOATINGPT, floatingpoint, CTLFLAG_RD,
    &vfp_exists, 0, "Floating point support enabled");

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
void
board_set_serial(uint64_t serial)
{

	snprintf(board_serial, sizeof(board_serial)-1, 
		    "%016jx", serial);
}

void
board_set_revision(uint32_t revision)
{

	board_revision = revision;
}

cognet's avatar
cognet committed
210
void
211
sendsig(catcher, ksi, mask)
cognet's avatar
cognet committed
212
	sig_t catcher;
213
	ksiginfo_t *ksi;
cognet's avatar
cognet committed
214
215
	sigset_t *mask;
{
kevlo's avatar
kevlo committed
216
217
218
	struct thread *td;
	struct proc *p;
	struct trapframe *tf;
cognet's avatar
cognet committed
219
	struct sigframe *fp, frame;
kevlo's avatar
kevlo committed
220
	struct sigacts *psp;
cognet's avatar
cognet committed
221
	int onstack;
222
223
	int sig;
	int code;
cognet's avatar
cognet committed
224

kevlo's avatar
kevlo committed
225
226
227
	td = curthread;
	p = td->td_proc;
	PROC_LOCK_ASSERT(p, MA_OWNED);
228
229
	sig = ksi->ksi_signo;
	code = ksi->ksi_code;
kevlo's avatar
kevlo committed
230
231
232
233
234
235
236
237
238
	psp = p->p_sigacts;
	mtx_assert(&psp->ps_mtx, MA_OWNED);
	tf = td->td_frame;
	onstack = sigonstack(tf->tf_usr_sp);

	CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
	    catcher, sig);

	/* Allocate and validate space for the signal handler context. */
239
	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !(onstack) &&
kevlo's avatar
kevlo committed
240
	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
imp's avatar
imp committed
241
		fp = (struct sigframe *)(td->td_sigstk.ss_sp +
kevlo's avatar
kevlo committed
242
243
		    td->td_sigstk.ss_size);
#if defined(COMPAT_43)
244
		td->td_sigstk.ss_flags |= SS_ONSTACK;
kevlo's avatar
kevlo committed
245
#endif
246
	} else
kevlo's avatar
kevlo committed
247
		fp = (struct sigframe *)td->td_frame->tf_usr_sp;
imp's avatar
imp committed
248

cognet's avatar
cognet committed
249
250
	/* make room on the stack */
	fp--;
Shawn Webb's avatar
Shawn Webb committed
251
	
cognet's avatar
cognet committed
252
	/* make the stack aligned */
cognet's avatar
cognet committed
253
	fp = (struct sigframe *)STACKALIGN(fp);
cognet's avatar
cognet committed
254
	/* Populate the siginfo frame. */
kevlo's avatar
kevlo committed
255
	get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
256
	frame.sf_si = ksi->ksi_info;
cognet's avatar
cognet committed
257
	frame.sf_uc.uc_sigmask = *mask;
imp's avatar
imp committed
258
	frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK )
259
	    ? ((onstack) ? SS_ONSTACK : 0) : SS_DISABLE;
cognet's avatar
cognet committed
260
	frame.sf_uc.uc_stack = td->td_sigstk;
cognet's avatar
cognet committed
261
	mtx_unlock(&psp->ps_mtx);
kevlo's avatar
kevlo committed
262
263
264
265
266
267
268
	PROC_UNLOCK(td->td_proc);

	/* Copy the sigframe out to the user's stack. */
	if (copyout(&frame, fp, sizeof(*fp)) != 0) {
		/* Process has trashed its stack. Kill it. */
		CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
		PROC_LOCK(p);
cognet's avatar
cognet committed
269
		sigexit(td, SIGILL);
kevlo's avatar
kevlo committed
270
271
272
273
274
275
	}

	/* Translate the signal if appropriate. */
	if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
		sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];

cognet's avatar
cognet committed
276
277
278
279
280
281
282
283
	/*
	 * Build context to run handler in.  We invoke the handler
	 * directly, only returning via the trampoline.  Note the
	 * trampoline version numbers are coordinated with machine-
	 * dependent code in libc.
	 */
	
	tf->tf_r0 = sig;
kevlo's avatar
kevlo committed
284
285
	tf->tf_r1 = (register_t)&fp->sf_si;
	tf->tf_r2 = (register_t)&fp->sf_uc;
cognet's avatar
cognet committed
286
287

	/* the trampoline uses r5 as the uc address */
kevlo's avatar
kevlo committed
288
289
290
291
	tf->tf_r5 = (register_t)&fp->sf_uc;
	tf->tf_pc = (register_t)catcher;
	tf->tf_usr_sp = (register_t)fp;
	tf->tf_usr_lr = (register_t)(PS_STRINGS - *(p->p_sysent->sv_szsigcode));
292
#ifdef PAX_ASLR
293
	pax_aslr_stack(td->td_proc, &tf->tf_usr_lr);
294
295
#endif

kevlo's avatar
kevlo committed
296
297
298
	CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_usr_lr,
	    tf->tf_usr_sp);

kevlo's avatar
kevlo committed
299
	PROC_LOCK(p);
cognet's avatar
cognet committed
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
	mtx_lock(&psp->ps_mtx);
}

struct kva_md_info kmi;

/*
 * arm32_vector_init:
 *
 *	Initialize the vector page, and select whether or not to
 *	relocate the vectors.
 *
 *	NOTE: We expect the vector page to be mapped at its expected
 *	destination.
 */

extern unsigned int page0[], page0_data[];
void
arm_vector_init(vm_offset_t va, int which)
{
	unsigned int *vectors = (int *) va;
	unsigned int *vectors_data = vectors + (page0_data - page0);
	int vec;

	/*
	 * Loop through the vectors we're taking over, and copy the
	 * vector's insn and data word.
	 */
	for (vec = 0; vec < ARM_NVEC; vec++) {
		if ((which & (1 << vec)) == 0) {
			/* Don't want to take over this vector. */
			continue;
		}
		vectors[vec] = page0[vec];
		vectors_data[vec] = page0_data[vec];
	}

	/* Now sync the vectors. */
	cpu_icache_sync_range(va, (ARM_NVEC * 2) * sizeof(u_int));

	vector_page = va;

	if (va == ARM_VECTORS_HIGH) {
		/*
		 * Assume the MD caller knows what it's doing here, and
		 * really does want the vector page relocated.
		 *
		 * Note: This has to be done here (and not just in
		 * cpu_setup()) because the vector page needs to be
		 * accessible *before* cpu_startup() is called.
		 * Think ddb(9) ...
		 *
		 * NOTE: If the CPU control register is not readable,
		 * this will totally fail!  We'll just assume that
		 * any system that has high vector support has a
		 * readable CPU control register, for now.  If we
		 * ever encounter one that does not, we'll have to
		 * rethink this.
		 */
		cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
	}
}

static void
cpu_startup(void *dummy)
{
	struct pcb *pcb = thread0.td_pcb;
366
	const unsigned int mbyte = 1024 * 1024;
gonzo's avatar
gonzo committed
367
#ifdef ARM_TP_ADDRESS
368
369
#ifndef ARM_CACHE_LOCK_ENABLE
	vm_page_t m;
gonzo's avatar
gonzo committed
370
#endif
371
#endif
372

373
374
	identify_arm_cpu();

375
	vm_ksubmap_init(&kmi);
376
377
378
379

	/*
	 * Display the RAM layout.
	 */
380
381
382
	printf("real memory  = %ju (%ju MB)\n", 
	    (uintmax_t)arm32_ptob(realmem),
	    (uintmax_t)arm32_ptob(realmem) / mbyte);
383
	printf("avail memory = %ju (%ju MB)\n",
384
385
	    (uintmax_t)arm32_ptob(vm_cnt.v_free_count),
	    (uintmax_t)arm32_ptob(vm_cnt.v_free_count) / mbyte);
386
387
	if (bootverbose) {
		arm_physmem_print_tables();
388
		arm_devmap_print_table();
389
	}
390

cognet's avatar
cognet committed
391
392
	bufinit();
	vm_pager_bufferinit();
ian's avatar
ian committed
393
	pcb->pcb_regs.sf_sp = (u_int)thread0.td_kstack +
cognet's avatar
cognet committed
394
395
	    USPACE_SVC_STACK_TOP;
	pmap_set_pcb_pagedir(pmap_kernel(), pcb);
396
397
#ifndef ARM_NEW_PMAP
	vector_page_setprot(VM_PROT_READ);
cognet's avatar
cognet committed
398
	pmap_postinit();
399
#endif
gonzo's avatar
gonzo committed
400
#ifdef ARM_TP_ADDRESS
401
402
403
404
405
406
407
#ifdef ARM_CACHE_LOCK_ENABLE
	pmap_kenter_user(ARM_TP_ADDRESS, ARM_TP_ADDRESS);
	arm_lock_cache_line(ARM_TP_ADDRESS);
#else
	m = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_ZERO);
	pmap_kenter_user(ARM_TP_ADDRESS, VM_PAGE_TO_PHYS(m));
#endif
408
409
	*(uint32_t *)ARM_RAS_START = 0;
	*(uint32_t *)ARM_RAS_END = 0xffffffff;
gonzo's avatar
gonzo committed
410
#endif
cognet's avatar
cognet committed
411
412
}

413
SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
cognet's avatar
cognet committed
414

415
416
417
418
419
420
421
422
423
/*
 * Flush the D-cache for non-DMA I/O so that the I-cache can
 * be made coherent later.
 */
void
cpu_flush_dcache(void *ptr, size_t len)
{

	cpu_dcache_wb_range((uintptr_t)ptr, len);
424
425
426
#ifdef ARM_L2_PIPT
	cpu_l2cache_wb_range((uintptr_t)vtophys(ptr), len);
#else
427
	cpu_l2cache_wb_range((uintptr_t)ptr, len);
428
#endif
429
430
}

431
432
433
434
435
436
437
438
/* Get current clock frequency for the given cpu id. */
int
cpu_est_clockrate(int cpu_id, uint64_t *rate)
{

	return (ENXIO);
}

cognet's avatar
cognet committed
439
void
440
cpu_idle(int busy)
cognet's avatar
cognet committed
441
{
gonzo's avatar
gonzo committed
442
	
ian's avatar
ian committed
443
	CTR2(KTR_SPARE2, "cpu_idle(%d) at %d", busy, curcpu);
444
	spinlock_enter();
gonzo's avatar
gonzo committed
445
#ifndef NO_EVENTTIMERS
ian's avatar
ian committed
446
	if (!busy)
gonzo's avatar
gonzo committed
447
448
		cpu_idleclock();
#endif
449
450
	if (!sched_runnable())
		cpu_sleep(0);
gonzo's avatar
gonzo committed
451
#ifndef NO_EVENTTIMERS
ian's avatar
ian committed
452
	if (!busy)
gonzo's avatar
gonzo committed
453
454
		cpu_activeclock();
#endif
455
	spinlock_exit();
ian's avatar
ian committed
456
	CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done", busy, curcpu);
cognet's avatar
cognet committed
457
458
}

459
460
461
462
463
464
465
int
cpu_idle_wakeup(int cpu)
{

	return (0);
}

466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
 * Most ARM platforms don't need to do anything special to init their clocks
 * (they get intialized during normal device attachment), and by not defining a
 * cpu_initclocks() function they get this generic one.  Any platform that needs
 * to do something special can just provide their own implementation, which will
 * override this one due to the weak linkage.
 */
void
arm_generic_initclocks(void)
{

#ifndef NO_EVENTTIMERS
#ifdef SMP
	if (PCPU_GET(cpuid) == 0)
		cpu_initclocks_bsp();
	else
		cpu_initclocks_ap();
#else
	cpu_initclocks_bsp();
#endif
#endif
}
__weak_reference(arm_generic_initclocks, cpu_initclocks);

cognet's avatar
cognet committed
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
int
fill_regs(struct thread *td, struct reg *regs)
{
	struct trapframe *tf = td->td_frame;
	bcopy(&tf->tf_r0, regs->r, sizeof(regs->r));
	regs->r_sp = tf->tf_usr_sp;
	regs->r_lr = tf->tf_usr_lr;
	regs->r_pc = tf->tf_pc;
	regs->r_cpsr = tf->tf_spsr;
	return (0);
}
int
fill_fpregs(struct thread *td, struct fpreg *regs)
{
	bzero(regs, sizeof(*regs));
	return (0);
}

int
set_regs(struct thread *td, struct reg *regs)
{
	struct trapframe *tf = td->td_frame;
	
513
	bcopy(regs->r, &tf->tf_r0, sizeof(regs->r));
cognet's avatar
cognet committed
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
	tf->tf_usr_sp = regs->r_sp;
	tf->tf_usr_lr = regs->r_lr;
	tf->tf_pc = regs->r_pc;
	tf->tf_spsr &=  ~PSR_FLAGS;
	tf->tf_spsr |= regs->r_cpsr & PSR_FLAGS;
	return (0);								
}

int
set_fpregs(struct thread *td, struct fpreg *regs)
{
	return (0);
}

int
fill_dbregs(struct thread *td, struct dbreg *regs)
{
	return (0);
}
int
set_dbregs(struct thread *td, struct dbreg *regs)
{
	return (0);
}

539
540
541
542
543
544

static int
ptrace_read_int(struct thread *td, vm_offset_t addr, u_int32_t *v)
{
	struct iovec iov;
	struct uio uio;
545
546

	PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
	iov.iov_base = (caddr_t) v;
	iov.iov_len = sizeof(u_int32_t);
	uio.uio_iov = &iov;
	uio.uio_iovcnt = 1;
	uio.uio_offset = (off_t)addr;
	uio.uio_resid = sizeof(u_int32_t);
	uio.uio_segflg = UIO_SYSSPACE;
	uio.uio_rw = UIO_READ;
	uio.uio_td = td;
	return proc_rwmem(td->td_proc, &uio);
}

static int
ptrace_write_int(struct thread *td, vm_offset_t addr, u_int32_t v)
{
	struct iovec iov;
	struct uio uio;
564
565

	PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
566
567
568
569
570
571
572
573
574
575
576
577
	iov.iov_base = (caddr_t) &v;
	iov.iov_len = sizeof(u_int32_t);
	uio.uio_iov = &iov;
	uio.uio_iovcnt = 1;
	uio.uio_offset = (off_t)addr;
	uio.uio_resid = sizeof(u_int32_t);
	uio.uio_segflg = UIO_SYSSPACE;
	uio.uio_rw = UIO_WRITE;
	uio.uio_td = td;
	return proc_rwmem(td->td_proc, &uio);
}

cognet's avatar
cognet committed
578
579
580
int
ptrace_single_step(struct thread *td)
{
581
	struct proc *p;
582
583
584
585
	int error;
	
	KASSERT(td->td_md.md_ptrace_instr == 0,
	 ("Didn't clear single step"));
586
587
	p = td->td_proc;
	PROC_UNLOCK(p);
imp's avatar
imp committed
588
	error = ptrace_read_int(td, td->td_frame->tf_pc + 4,
589
590
	    &td->td_md.md_ptrace_instr);
	if (error)
591
		goto out;
592
593
594
595
596
	error = ptrace_write_int(td, td->td_frame->tf_pc + 4,
	    PTRACE_BREAKPOINT);
	if (error)
		td->td_md.md_ptrace_instr = 0;
	td->td_md.md_ptrace_addr = td->td_frame->tf_pc + 4;
597
598
out:
	PROC_LOCK(p);
599
	return (error);
cognet's avatar
cognet committed
600
601
602
603
604
}

int
ptrace_clear_single_step(struct thread *td)
{
605
606
	struct proc *p;

607
	if (td->td_md.md_ptrace_instr) {
608
609
		p = td->td_proc;
		PROC_UNLOCK(p);
610
611
		ptrace_write_int(td, td->td_md.md_ptrace_addr,
		    td->td_md.md_ptrace_instr);
612
		PROC_LOCK(p);
613
614
		td->td_md.md_ptrace_instr = 0;
	}
cognet's avatar
cognet committed
615
616
617
618
619
620
	return (0);
}

int
ptrace_set_pc(struct thread *td, unsigned long addr)
{
cognet's avatar
cognet committed
621
	td->td_frame->tf_pc = addr;
cognet's avatar
cognet committed
622
623
624
625
626
627
628
629
	return (0);
}

void
cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
{
}

630
631
632
633
void
spinlock_enter(void)
{
	struct thread *td;
634
	register_t cspr;
635
636

	td = curthread;
637
	if (td->td_md.md_spinlock_count == 0) {
638
		cspr = disable_interrupts(PSR_I | PSR_F);
639
640
641
642
		td->td_md.md_spinlock_count = 1;
		td->td_md.md_saved_cspr = cspr;
	} else
		td->td_md.md_spinlock_count++;
643
644
645
646
647
648
649
	critical_enter();
}

void
spinlock_exit(void)
{
	struct thread *td;
650
	register_t cspr;
651
652
653

	td = curthread;
	critical_exit();
654
	cspr = td->td_md.md_saved_cspr;
655
656
	td->td_md.md_spinlock_count--;
	if (td->td_md.md_spinlock_count == 0)
657
		restore_interrupts(cspr);
658
659
}

cognet's avatar
cognet committed
660
661
662
663
/*
 * Clear registers on exec
 */
void
664
exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
cognet's avatar
cognet committed
665
666
667
668
669
{
	struct trapframe *tf = td->td_frame;

	memset(tf, 0, sizeof(*tf));
	tf->tf_usr_sp = stack;
670
	tf->tf_usr_lr = imgp->entry_addr;
cognet's avatar
cognet committed
671
	tf->tf_svc_lr = 0x77777777;
672
	tf->tf_pc = imgp->entry_addr;
cognet's avatar
cognet committed
673
674
675
676
677
678
679
680
681
682
683
684
	tf->tf_spsr = PSR_USR32_MODE;
}

/*
 * Get machine context.
 */
int
get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
{
	struct trapframe *tf = td->td_frame;
	__greg_t *gr = mcp->__gregs;

685
686
687
688
	if (clear_ret & GET_MC_CLEAR_RET)
		gr[_REG_R0] = 0;
	else
		gr[_REG_R0]   = tf->tf_r0;
cognet's avatar
cognet committed
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
	gr[_REG_R1]   = tf->tf_r1;
	gr[_REG_R2]   = tf->tf_r2;
	gr[_REG_R3]   = tf->tf_r3;
	gr[_REG_R4]   = tf->tf_r4;
	gr[_REG_R5]   = tf->tf_r5;
	gr[_REG_R6]   = tf->tf_r6;
	gr[_REG_R7]   = tf->tf_r7;
	gr[_REG_R8]   = tf->tf_r8;
	gr[_REG_R9]   = tf->tf_r9;
	gr[_REG_R10]  = tf->tf_r10;
	gr[_REG_R11]  = tf->tf_r11;
	gr[_REG_R12]  = tf->tf_r12;
	gr[_REG_SP]   = tf->tf_usr_sp;
	gr[_REG_LR]   = tf->tf_usr_lr;
	gr[_REG_PC]   = tf->tf_pc;
	gr[_REG_CPSR] = tf->tf_spsr;

	return (0);
}

/*
 * Set machine context.
 *
 * However, we don't set any but the user modifiable flags, and we won't
 * touch the cs selector.
 */
int
716
set_mcontext(struct thread *td, mcontext_t *mcp)
cognet's avatar
cognet committed
717
{
718
	struct trapframe *tf = td->td_frame;
cognet's avatar
cognet committed
719
	const __greg_t *gr = mcp->__gregs;
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738

	tf->tf_r0 = gr[_REG_R0];
	tf->tf_r1 = gr[_REG_R1];
	tf->tf_r2 = gr[_REG_R2];
	tf->tf_r3 = gr[_REG_R3];
	tf->tf_r4 = gr[_REG_R4];
	tf->tf_r5 = gr[_REG_R5];
	tf->tf_r6 = gr[_REG_R6];
	tf->tf_r7 = gr[_REG_R7];
	tf->tf_r8 = gr[_REG_R8];
	tf->tf_r9 = gr[_REG_R9];
	tf->tf_r10 = gr[_REG_R10];
	tf->tf_r11 = gr[_REG_R11];
	tf->tf_r12 = gr[_REG_R12];
	tf->tf_usr_sp = gr[_REG_SP];
	tf->tf_usr_lr = gr[_REG_LR];
	tf->tf_pc = gr[_REG_PC];
	tf->tf_spsr = gr[_REG_CPSR];

cognet's avatar
cognet committed
739
740
741
742
743
744
745
	return (0);
}

/*
 * MPSAFE
 */
int
746
sys_sigreturn(td, uap)
cognet's avatar
cognet committed
747
748
	struct thread *td;
	struct sigreturn_args /* {
749
		const struct __ucontext *sigcntxp;
cognet's avatar
cognet committed
750
751
	} */ *uap;
{
752
	ucontext_t uc;
cognet's avatar
cognet committed
753
754
755
756
	int spsr;
	
	if (uap == NULL)
		return (EFAULT);
757
	if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
cognet's avatar
cognet committed
758
759
760
761
762
		return (EFAULT);
	/*
	 * Make sure the processor mode has not been tampered with and
	 * interrupts have not been disabled.
	 */
763
	spsr = uc.uc_mcontext.__gregs[_REG_CPSR];
cognet's avatar
cognet committed
764
	if ((spsr & PSR_MODE) != PSR_USR32_MODE ||
765
	    (spsr & (PSR_I | PSR_F)) != 0)
cognet's avatar
cognet committed
766
767
		return (EINVAL);
		/* Restore register context. */
768
	set_mcontext(td, &uc.uc_mcontext);
cognet's avatar
cognet committed
769
770

	/* Restore signal mask. */
771
	kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
cognet's avatar
cognet committed
772
773

	return (EJUSTRETURN);
cognet's avatar
cognet committed
774
775
776
}


cognet's avatar
cognet committed
777
778
779
780
781
782
783
784
785
786
/*
 * Construct a PCB from a trapframe. This is called from kdb_trap() where
 * we want to start a backtrace from the function that caused us to enter
 * the debugger. We have the context in the trapframe, but base the trace
 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
 * enough for a backtrace.
 */
void
makectx(struct trapframe *tf, struct pcb *pcb)
{
ian's avatar
ian committed
787
788
789
790
791
792
793
794
795
796
797
798
	pcb->pcb_regs.sf_r4 = tf->tf_r4;
	pcb->pcb_regs.sf_r5 = tf->tf_r5;
	pcb->pcb_regs.sf_r6 = tf->tf_r6;
	pcb->pcb_regs.sf_r7 = tf->tf_r7;
	pcb->pcb_regs.sf_r8 = tf->tf_r8;
	pcb->pcb_regs.sf_r9 = tf->tf_r9;
	pcb->pcb_regs.sf_r10 = tf->tf_r10;
	pcb->pcb_regs.sf_r11 = tf->tf_r11;
	pcb->pcb_regs.sf_r12 = tf->tf_r12;
	pcb->pcb_regs.sf_pc = tf->tf_pc;
	pcb->pcb_regs.sf_lr = tf->tf_usr_lr;
	pcb->pcb_regs.sf_sp = tf->tf_usr_sp;
cognet's avatar
cognet committed
799
}
800
801
802
803
804

/*
 * Fake up a boot descriptor table
 */
vm_offset_t
805
fake_preload_metadata(struct arm_boot_params *abp __unused)
806
807
808
809
810
811
812
813
814
{
#ifdef DDB
	vm_offset_t zstart = 0, zend = 0;
#endif
	vm_offset_t lastaddr;
	int i = 0;
	static uint32_t fake_preload[35];

	fake_preload[i++] = MODINFO_NAME;
fabient's avatar
fabient committed
815
816
817
	fake_preload[i++] = strlen("kernel") + 1;
	strcpy((char*)&fake_preload[i++], "kernel");
	i += 1;
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
	fake_preload[i++] = MODINFO_TYPE;
	fake_preload[i++] = strlen("elf kernel") + 1;
	strcpy((char*)&fake_preload[i++], "elf kernel");
	i += 2;
	fake_preload[i++] = MODINFO_ADDR;
	fake_preload[i++] = sizeof(vm_offset_t);
	fake_preload[i++] = KERNVIRTADDR;
	fake_preload[i++] = MODINFO_SIZE;
	fake_preload[i++] = sizeof(uint32_t);
	fake_preload[i++] = (uint32_t)&end - KERNVIRTADDR;
#ifdef DDB
	if (*(uint32_t *)KERNVIRTADDR == MAGIC_TRAMP_NUMBER) {
		fake_preload[i++] = MODINFO_METADATA|MODINFOMD_SSYM;
		fake_preload[i++] = sizeof(vm_offset_t);
		fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 4);
		fake_preload[i++] = MODINFO_METADATA|MODINFOMD_ESYM;
		fake_preload[i++] = sizeof(vm_offset_t);
		fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 8);
		lastaddr = *(uint32_t *)(KERNVIRTADDR + 8);
		zend = lastaddr;
		zstart = *(uint32_t *)(KERNVIRTADDR + 4);
839
		db_fetch_ksymtab(zstart, zend);
840
841
842
843
844
845
846
847
848
	} else
#endif
		lastaddr = (vm_offset_t)&end;
	fake_preload[i++] = 0;
	fake_preload[i] = 0;
	preload_metadata = (void *)fake_preload;

	return (lastaddr);
}
849

gonzo's avatar
gonzo committed
850
851
852
void
pcpu0_init(void)
{
853
#if ARM_ARCH_6 || ARM_ARCH_7A || defined(CPU_MV_PJ4B)
854
	set_curthread(&thread0);
gonzo's avatar
gonzo committed
855
856
857
#endif
	pcpu_init(pcpup, 0, sizeof(struct pcpu));
	PCPU_SET(curthread, &thread0);
858
#ifdef VFP
gonzo's avatar
gonzo committed
859
860
861
862
	PCPU_SET(cpu, 0);
#endif
}

863
#if defined(LINUX_BOOT_ABI)
864
vm_offset_t
865
linux_parse_boot_param(struct arm_boot_params *abp)
866
{
867
	struct arm_lbabi_tag *walker;
868
869
	uint32_t revision;
	uint64_t serial;
870
871
872
873
874
875
876
877
878
879
880

	/*
	 * Linux boot ABI: r0 = 0, r1 is the board type (!= 0) and r2
	 * is atags or dtb pointer.  If all of these aren't satisfied,
	 * then punt.
	 */
	if (!(abp->abp_r0 == 0 && abp->abp_r1 != 0 && abp->abp_r2 != 0))
		return 0;

	board_id = abp->abp_r1;
	walker = (struct arm_lbabi_tag *)
881
	    (abp->abp_r2 + KERNVIRTADDR - abp->abp_physaddr);
882
883
884
885
886
887
888
889
890
891
892

	/* xxx - Need to also look for binary device tree */
	if (ATAG_TAG(walker) != ATAG_CORE)
		return 0;

	atag_list = walker;
	while (ATAG_TAG(walker) != ATAG_NONE) {
		switch (ATAG_TAG(walker)) {
		case ATAG_CORE:
			break;
		case ATAG_MEM:
893
894
			arm_physmem_hardware_region(walker->u.tag_mem.start,
			    walker->u.tag_mem.size);
895
896
897
898
899
900
			break;
		case ATAG_INITRD2:
			break;
		case ATAG_SERIAL:
			serial = walker->u.tag_sn.low |
			    ((uint64_t)walker->u.tag_sn.high << 32);
901
			board_set_serial(serial);
902
903
904
			break;
		case ATAG_REVISION:
			revision = walker->u.tag_rev.rev;
905
			board_set_revision(revision);
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
			break;
		case ATAG_CMDLINE:
			/* XXX open question: Parse this for boothowto? */
			bcopy(walker->u.tag_cmd.command, linux_command_line,
			      ATAG_SIZE(walker));
			break;
		default:
			break;
		}
		walker = ATAG_NEXT(walker);
	}

	/* Save a copy for later */
	bcopy(atag_list, atags,
	    (char *)walker - (char *)atag_list + ATAG_SIZE(walker));

	return fake_preload_metadata(abp);
}
#endif

926
#if defined(FREEBSD_BOOT_LOADER)
927
928
929
930
vm_offset_t
freebsd_parse_boot_param(struct arm_boot_params *abp)
{
	vm_offset_t lastaddr = 0;
931
932
	void *mdp;
	void *kmdp;
933
934
935
936
#ifdef DDB
	vm_offset_t ksym_start;
	vm_offset_t ksym_end;
#endif
937
938
939
940
941

	/*
	 * Mask metadata pointer: it is supposed to be on page boundary. If
	 * the first argument (mdp) doesn't point to a valid address the
	 * bootloader must have passed us something else than the metadata
942
943
	 * ptr, so we give up.  Also give up if we cannot find metadta section
	 * the loader creates that we get all this data out of.
944
	 */
945
946
947
948
949
950
951
952
953
954
955

	if ((mdp = (void *)(abp->abp_r0 & ~PAGE_MASK)) == NULL)
		return 0;
	preload_metadata = mdp;
	kmdp = preload_search_by_type("elf kernel");
	if (kmdp == NULL)
		return 0;

	boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
	kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
	lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
956
#ifdef DDB
957
958
	ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
	ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
959
	db_fetch_ksymtab(ksym_start, ksym_end);
960
#endif
961
	preload_addr_relocate = KERNVIRTADDR - abp->abp_physaddr;
962
963
	return lastaddr;
}
964
965
#endif

966
967
968
969
970
971
972
973
vm_offset_t
default_parse_boot_param(struct arm_boot_params *abp)
{
	vm_offset_t lastaddr;

#if defined(LINUX_BOOT_ABI)
	if ((lastaddr = linux_parse_boot_param(abp)) != 0)
		return lastaddr;
974
#endif
975
976
977
978
979
980
981
#if defined(FREEBSD_BOOT_LOADER)
	if ((lastaddr = freebsd_parse_boot_param(abp)) != 0)
		return lastaddr;
#endif
	/* Fall back to hardcoded metadata. */
	lastaddr = fake_preload_metadata(abp);

982
983
984
	return lastaddr;
}

985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
/*
 * Stub version of the boot parameter parsing routine.  We are
 * called early in initarm, before even VM has been initialized.
 * This routine needs to preserve any data that the boot loader
 * has passed in before the kernel starts to grow past the end
 * of the BSS, traditionally the place boot-loaders put this data.
 *
 * Since this is called so early, things that depend on the vm system
 * being setup (including access to some SoC's serial ports), about
 * all that can be done in this routine is to copy the arguments.
 *
 * This is the default boot parameter parsing routine.  Individual
 * kernels/boards can override this weak function with one of their
 * own.  We just fake metadata...
 */
1000
__weak_reference(default_parse_boot_param, parse_boot_param);