1/*
2 * Code to prepare detour buffer for optprobes in Kernel.
3 *
4 * Copyright 2017, Anju T, IBM Corp.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <asm/ppc_asm.h>
13#include <asm/ptrace.h>
14#include <asm/asm-offsets.h>
15
16#define	OPT_SLOT_SIZE	65536
17
18	.balign	4
19
20	/*
21	 * Reserve an area to allocate slots for detour buffer.
22	 * This is part of .text section (rather than vmalloc area)
23	 * as this needs to be within 32MB of the probed address.
24	 */
25	.global optinsn_slot
26optinsn_slot:
27	.space	OPT_SLOT_SIZE
28
29	/*
30	 * Optprobe template:
31	 * This template gets copied into one of the slots in optinsn_slot
32	 * and gets fixed up with real optprobe structures et al.
33	 */
34	.global optprobe_template_entry
35optprobe_template_entry:
36	/* Create an in-memory pt_regs */
37	stdu	r1,-INT_FRAME_SIZE(r1)
38	SAVE_GPR(0,r1)
39	/* Save the previous SP into stack */
40	addi	r0,r1,INT_FRAME_SIZE
41	std	r0,GPR1(r1)
42	SAVE_10GPRS(2,r1)
43	SAVE_10GPRS(12,r1)
44	SAVE_10GPRS(22,r1)
45	/* Save SPRS */
46	mfmsr	r5
47	std	r5,_MSR(r1)
48	li	r5,0x700
49	std	r5,_TRAP(r1)
50	li	r5,0
51	std	r5,ORIG_GPR3(r1)
52	std	r5,RESULT(r1)
53	mfctr	r5
54	std	r5,_CTR(r1)
55	mflr	r5
56	std	r5,_LINK(r1)
57	mfspr	r5,SPRN_XER
58	std	r5,_XER(r1)
59	mfcr	r5
60	std	r5,_CCR(r1)
61	lbz     r5,PACASOFTIRQEN(r13)
62	std     r5,SOFTE(r1)
63	mfdar	r5
64	std	r5,_DAR(r1)
65	mfdsisr	r5
66	std	r5,_DSISR(r1)
67
68	.global optprobe_template_op_address
69optprobe_template_op_address:
70	/*
71	 * Parameters to optimized_callback():
72	 * 1. optimized_kprobe structure in r3
73	 */
74	nop
75	nop
76	nop
77	nop
78	nop
79	/* 2. pt_regs pointer in r4 */
80	addi	r4,r1,STACK_FRAME_OVERHEAD
81
82	.global optprobe_template_call_handler
83optprobe_template_call_handler:
84	/* Branch to optimized_callback() */
85	nop
86
87	/*
88	 * Parameters for instruction emulation:
89	 * 1. Pass SP in register r3.
90	 */
91	addi	r3,r1,STACK_FRAME_OVERHEAD
92
93	.global optprobe_template_insn
94optprobe_template_insn:
95	/* 2, Pass instruction to be emulated in r4 */
96	nop
97	nop
98
99	.global optprobe_template_call_emulate
100optprobe_template_call_emulate:
101	/* Branch to emulate_step()  */
102	nop
103
104	/*
105	 * All done.
106	 * Now, restore the registers...
107	 */
108	ld	r5,_MSR(r1)
109	mtmsr	r5
110	ld	r5,_CTR(r1)
111	mtctr	r5
112	ld	r5,_LINK(r1)
113	mtlr	r5
114	ld	r5,_XER(r1)
115	mtxer	r5
116	ld	r5,_CCR(r1)
117	mtcr	r5
118	ld	r5,_DAR(r1)
119	mtdar	r5
120	ld	r5,_DSISR(r1)
121	mtdsisr	r5
122	REST_GPR(0,r1)
123	REST_10GPRS(2,r1)
124	REST_10GPRS(12,r1)
125	REST_10GPRS(22,r1)
126	/* Restore the previous SP */
127	addi	r1,r1,INT_FRAME_SIZE
128
129	.global optprobe_template_ret
130optprobe_template_ret:
131	/* ... and jump back from trampoline */
132	nop
133
134	.global optprobe_template_end
135optprobe_template_end:
136