xref: /kvm-unit-tests/lib/powerpc/processor.c (revision 5d5710978d19e5b74e2a0872731612298899f639)
1 /*
2  * processor control and status function
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Library General Public License version 2.
6  */
7 
8 #include <libcflat.h>
9 #include <asm/processor.h>
10 #include <asm/ptrace.h>
11 #include <asm/setup.h>
12 #include <asm/barrier.h>
13 
14 static struct {
15 	void (*func)(struct pt_regs *, void *data);
16 	void *data;
17 } handlers[16];
18 
19 void handle_exception(int trap, void (*func)(struct pt_regs *, void *),
20 		      void * data)
21 {
22 	assert(!(trap & ~0xf00));
23 
24 	trap >>= 8;
25 
26 	if (func && handlers[trap].func) {
27 		printf("exception handler installed twice %#x\n", trap);
28 		abort();
29 	}
30 	handlers[trap].func = func;
31 	handlers[trap].data = data;
32 }
33 
34 void do_handle_exception(struct pt_regs *regs)
35 {
36 	unsigned char v;
37 
38 	v = regs->trap >> 8;
39 
40 	if (v < 16 && handlers[v].func) {
41 		handlers[v].func(regs, handlers[v].data);
42 		return;
43 	}
44 
45 	printf("unhandled cpu exception %#lx at NIA:0x%016lx MSR:0x%016lx\n", regs->trap, regs->nip, regs->msr);
46 	abort();
47 }
48 
49 void delay(uint64_t cycles)
50 {
51 	uint64_t start = get_tb();
52 
53 	while ((get_tb() - start) < cycles)
54 		cpu_relax();
55 }
56 
57 void udelay(uint64_t us)
58 {
59 	delay((us * tb_hz) / 1000000);
60 }
61