1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * asm-offsets.c: Calculate pt_regs and task_struct offsets.
4  *
5  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
6  */
7 #include <linux/types.h>
8 #include <linux/sched.h>
9 #include <linux/mm.h>
10 #include <linux/kbuild.h>
11 #include <linux/suspend.h>
12 #include <asm/cpu-info.h>
13 #include <asm/ptrace.h>
14 #include <asm/processor.h>
15 #include <asm/ftrace.h>
16 
17 void output_ptreg_defines(void)
18 {
19 	COMMENT("LoongArch pt_regs offsets.");
20 	OFFSET(PT_R0, pt_regs, regs[0]);
21 	OFFSET(PT_R1, pt_regs, regs[1]);
22 	OFFSET(PT_R2, pt_regs, regs[2]);
23 	OFFSET(PT_R3, pt_regs, regs[3]);
24 	OFFSET(PT_R4, pt_regs, regs[4]);
25 	OFFSET(PT_R5, pt_regs, regs[5]);
26 	OFFSET(PT_R6, pt_regs, regs[6]);
27 	OFFSET(PT_R7, pt_regs, regs[7]);
28 	OFFSET(PT_R8, pt_regs, regs[8]);
29 	OFFSET(PT_R9, pt_regs, regs[9]);
30 	OFFSET(PT_R10, pt_regs, regs[10]);
31 	OFFSET(PT_R11, pt_regs, regs[11]);
32 	OFFSET(PT_R12, pt_regs, regs[12]);
33 	OFFSET(PT_R13, pt_regs, regs[13]);
34 	OFFSET(PT_R14, pt_regs, regs[14]);
35 	OFFSET(PT_R15, pt_regs, regs[15]);
36 	OFFSET(PT_R16, pt_regs, regs[16]);
37 	OFFSET(PT_R17, pt_regs, regs[17]);
38 	OFFSET(PT_R18, pt_regs, regs[18]);
39 	OFFSET(PT_R19, pt_regs, regs[19]);
40 	OFFSET(PT_R20, pt_regs, regs[20]);
41 	OFFSET(PT_R21, pt_regs, regs[21]);
42 	OFFSET(PT_R22, pt_regs, regs[22]);
43 	OFFSET(PT_R23, pt_regs, regs[23]);
44 	OFFSET(PT_R24, pt_regs, regs[24]);
45 	OFFSET(PT_R25, pt_regs, regs[25]);
46 	OFFSET(PT_R26, pt_regs, regs[26]);
47 	OFFSET(PT_R27, pt_regs, regs[27]);
48 	OFFSET(PT_R28, pt_regs, regs[28]);
49 	OFFSET(PT_R29, pt_regs, regs[29]);
50 	OFFSET(PT_R30, pt_regs, regs[30]);
51 	OFFSET(PT_R31, pt_regs, regs[31]);
52 	OFFSET(PT_CRMD, pt_regs, csr_crmd);
53 	OFFSET(PT_PRMD, pt_regs, csr_prmd);
54 	OFFSET(PT_EUEN, pt_regs, csr_euen);
55 	OFFSET(PT_ECFG, pt_regs, csr_ecfg);
56 	OFFSET(PT_ESTAT, pt_regs, csr_estat);
57 	OFFSET(PT_ERA, pt_regs, csr_era);
58 	OFFSET(PT_BVADDR, pt_regs, csr_badvaddr);
59 	OFFSET(PT_ORIG_A0, pt_regs, orig_a0);
60 	DEFINE(PT_SIZE, sizeof(struct pt_regs));
61 	BLANK();
62 }
63 
64 void output_task_defines(void)
65 {
66 	COMMENT("LoongArch task_struct offsets.");
67 	OFFSET(TASK_STATE, task_struct, __state);
68 	OFFSET(TASK_THREAD_INFO, task_struct, stack);
69 	OFFSET(TASK_FLAGS, task_struct, flags);
70 	OFFSET(TASK_MM, task_struct, mm);
71 	OFFSET(TASK_PID, task_struct, pid);
72 #if defined(CONFIG_STACKPROTECTOR)
73 	OFFSET(TASK_STACK_CANARY, task_struct, stack_canary);
74 #endif
75 	DEFINE(TASK_STRUCT_SIZE, sizeof(struct task_struct));
76 	BLANK();
77 }
78 
79 void output_thread_info_defines(void)
80 {
81 	COMMENT("LoongArch thread_info offsets.");
82 	OFFSET(TI_TASK, thread_info, task);
83 	OFFSET(TI_FLAGS, thread_info, flags);
84 	OFFSET(TI_TP_VALUE, thread_info, tp_value);
85 	OFFSET(TI_CPU, thread_info, cpu);
86 	OFFSET(TI_PRE_COUNT, thread_info, preempt_count);
87 	OFFSET(TI_REGS, thread_info, regs);
88 	DEFINE(_THREAD_SIZE, THREAD_SIZE);
89 	DEFINE(_THREAD_MASK, THREAD_MASK);
90 	DEFINE(_IRQ_STACK_SIZE, IRQ_STACK_SIZE);
91 	DEFINE(_IRQ_STACK_START, IRQ_STACK_START);
92 	BLANK();
93 }
94 
95 void output_thread_defines(void)
96 {
97 	COMMENT("LoongArch specific thread_struct offsets.");
98 	OFFSET(THREAD_REG01, task_struct, thread.reg01);
99 	OFFSET(THREAD_REG03, task_struct, thread.reg03);
100 	OFFSET(THREAD_REG22, task_struct, thread.reg22);
101 	OFFSET(THREAD_REG23, task_struct, thread.reg23);
102 	OFFSET(THREAD_REG24, task_struct, thread.reg24);
103 	OFFSET(THREAD_REG25, task_struct, thread.reg25);
104 	OFFSET(THREAD_REG26, task_struct, thread.reg26);
105 	OFFSET(THREAD_REG27, task_struct, thread.reg27);
106 	OFFSET(THREAD_REG28, task_struct, thread.reg28);
107 	OFFSET(THREAD_REG29, task_struct, thread.reg29);
108 	OFFSET(THREAD_REG30, task_struct, thread.reg30);
109 	OFFSET(THREAD_REG31, task_struct, thread.reg31);
110 	OFFSET(THREAD_SCHED_RA, task_struct, thread.sched_ra);
111 	OFFSET(THREAD_SCHED_CFA, task_struct, thread.sched_cfa);
112 	OFFSET(THREAD_CSRCRMD, task_struct,
113 	       thread.csr_crmd);
114 	OFFSET(THREAD_CSRPRMD, task_struct,
115 	       thread.csr_prmd);
116 	OFFSET(THREAD_CSREUEN, task_struct,
117 	       thread.csr_euen);
118 	OFFSET(THREAD_CSRECFG, task_struct,
119 	       thread.csr_ecfg);
120 
121 	OFFSET(THREAD_FPU, task_struct, thread.fpu);
122 
123 	OFFSET(THREAD_BVADDR, task_struct, \
124 	       thread.csr_badvaddr);
125 	OFFSET(THREAD_ECODE, task_struct, \
126 	       thread.error_code);
127 	OFFSET(THREAD_TRAPNO, task_struct, thread.trap_nr);
128 	BLANK();
129 }
130 
131 void output_thread_fpu_defines(void)
132 {
133 	OFFSET(THREAD_FPR0, loongarch_fpu, fpr[0]);
134 	OFFSET(THREAD_FPR1, loongarch_fpu, fpr[1]);
135 	OFFSET(THREAD_FPR2, loongarch_fpu, fpr[2]);
136 	OFFSET(THREAD_FPR3, loongarch_fpu, fpr[3]);
137 	OFFSET(THREAD_FPR4, loongarch_fpu, fpr[4]);
138 	OFFSET(THREAD_FPR5, loongarch_fpu, fpr[5]);
139 	OFFSET(THREAD_FPR6, loongarch_fpu, fpr[6]);
140 	OFFSET(THREAD_FPR7, loongarch_fpu, fpr[7]);
141 	OFFSET(THREAD_FPR8, loongarch_fpu, fpr[8]);
142 	OFFSET(THREAD_FPR9, loongarch_fpu, fpr[9]);
143 	OFFSET(THREAD_FPR10, loongarch_fpu, fpr[10]);
144 	OFFSET(THREAD_FPR11, loongarch_fpu, fpr[11]);
145 	OFFSET(THREAD_FPR12, loongarch_fpu, fpr[12]);
146 	OFFSET(THREAD_FPR13, loongarch_fpu, fpr[13]);
147 	OFFSET(THREAD_FPR14, loongarch_fpu, fpr[14]);
148 	OFFSET(THREAD_FPR15, loongarch_fpu, fpr[15]);
149 	OFFSET(THREAD_FPR16, loongarch_fpu, fpr[16]);
150 	OFFSET(THREAD_FPR17, loongarch_fpu, fpr[17]);
151 	OFFSET(THREAD_FPR18, loongarch_fpu, fpr[18]);
152 	OFFSET(THREAD_FPR19, loongarch_fpu, fpr[19]);
153 	OFFSET(THREAD_FPR20, loongarch_fpu, fpr[20]);
154 	OFFSET(THREAD_FPR21, loongarch_fpu, fpr[21]);
155 	OFFSET(THREAD_FPR22, loongarch_fpu, fpr[22]);
156 	OFFSET(THREAD_FPR23, loongarch_fpu, fpr[23]);
157 	OFFSET(THREAD_FPR24, loongarch_fpu, fpr[24]);
158 	OFFSET(THREAD_FPR25, loongarch_fpu, fpr[25]);
159 	OFFSET(THREAD_FPR26, loongarch_fpu, fpr[26]);
160 	OFFSET(THREAD_FPR27, loongarch_fpu, fpr[27]);
161 	OFFSET(THREAD_FPR28, loongarch_fpu, fpr[28]);
162 	OFFSET(THREAD_FPR29, loongarch_fpu, fpr[29]);
163 	OFFSET(THREAD_FPR30, loongarch_fpu, fpr[30]);
164 	OFFSET(THREAD_FPR31, loongarch_fpu, fpr[31]);
165 
166 	OFFSET(THREAD_FCSR, loongarch_fpu, fcsr);
167 	OFFSET(THREAD_FCC,  loongarch_fpu, fcc);
168 	OFFSET(THREAD_FTOP, loongarch_fpu, ftop);
169 	BLANK();
170 }
171 
172 void output_thread_lbt_defines(void)
173 {
174 	OFFSET(THREAD_SCR0,  loongarch_lbt, scr0);
175 	OFFSET(THREAD_SCR1,  loongarch_lbt, scr1);
176 	OFFSET(THREAD_SCR2,  loongarch_lbt, scr2);
177 	OFFSET(THREAD_SCR3,  loongarch_lbt, scr3);
178 	OFFSET(THREAD_EFLAGS, loongarch_lbt, eflags);
179 	BLANK();
180 }
181 
182 void output_mm_defines(void)
183 {
184 	COMMENT("Size of struct page");
185 	DEFINE(STRUCT_PAGE_SIZE, sizeof(struct page));
186 	BLANK();
187 	COMMENT("Linux mm_struct offsets.");
188 	OFFSET(MM_USERS, mm_struct, mm_users);
189 	OFFSET(MM_PGD, mm_struct, pgd);
190 	OFFSET(MM_CONTEXT, mm_struct, context);
191 	BLANK();
192 	DEFINE(_PGD_T_SIZE, sizeof(pgd_t));
193 	DEFINE(_PMD_T_SIZE, sizeof(pmd_t));
194 	DEFINE(_PTE_T_SIZE, sizeof(pte_t));
195 	BLANK();
196 	DEFINE(_PGD_T_LOG2, PGD_T_LOG2);
197 #ifndef __PAGETABLE_PMD_FOLDED
198 	DEFINE(_PMD_T_LOG2, PMD_T_LOG2);
199 #endif
200 	DEFINE(_PTE_T_LOG2, PTE_T_LOG2);
201 	BLANK();
202 	DEFINE(_PMD_SHIFT, PMD_SHIFT);
203 	DEFINE(_PGDIR_SHIFT, PGDIR_SHIFT);
204 	BLANK();
205 	DEFINE(_PTRS_PER_PGD, PTRS_PER_PGD);
206 	DEFINE(_PTRS_PER_PMD, PTRS_PER_PMD);
207 	DEFINE(_PTRS_PER_PTE, PTRS_PER_PTE);
208 	BLANK();
209 	DEFINE(_PAGE_SHIFT, PAGE_SHIFT);
210 	DEFINE(_PAGE_SIZE, PAGE_SIZE);
211 	BLANK();
212 }
213 
214 void output_sc_defines(void)
215 {
216 	COMMENT("Linux sigcontext offsets.");
217 	OFFSET(SC_REGS, sigcontext, sc_regs);
218 	OFFSET(SC_PC, sigcontext, sc_pc);
219 	BLANK();
220 }
221 
222 void output_signal_defines(void)
223 {
224 	COMMENT("Linux signal numbers.");
225 	DEFINE(_SIGHUP, SIGHUP);
226 	DEFINE(_SIGINT, SIGINT);
227 	DEFINE(_SIGQUIT, SIGQUIT);
228 	DEFINE(_SIGILL, SIGILL);
229 	DEFINE(_SIGTRAP, SIGTRAP);
230 	DEFINE(_SIGIOT, SIGIOT);
231 	DEFINE(_SIGABRT, SIGABRT);
232 	DEFINE(_SIGFPE, SIGFPE);
233 	DEFINE(_SIGKILL, SIGKILL);
234 	DEFINE(_SIGBUS, SIGBUS);
235 	DEFINE(_SIGSEGV, SIGSEGV);
236 	DEFINE(_SIGSYS, SIGSYS);
237 	DEFINE(_SIGPIPE, SIGPIPE);
238 	DEFINE(_SIGALRM, SIGALRM);
239 	DEFINE(_SIGTERM, SIGTERM);
240 	DEFINE(_SIGUSR1, SIGUSR1);
241 	DEFINE(_SIGUSR2, SIGUSR2);
242 	DEFINE(_SIGCHLD, SIGCHLD);
243 	DEFINE(_SIGPWR, SIGPWR);
244 	DEFINE(_SIGWINCH, SIGWINCH);
245 	DEFINE(_SIGURG, SIGURG);
246 	DEFINE(_SIGIO, SIGIO);
247 	DEFINE(_SIGSTOP, SIGSTOP);
248 	DEFINE(_SIGTSTP, SIGTSTP);
249 	DEFINE(_SIGCONT, SIGCONT);
250 	DEFINE(_SIGTTIN, SIGTTIN);
251 	DEFINE(_SIGTTOU, SIGTTOU);
252 	DEFINE(_SIGVTALRM, SIGVTALRM);
253 	DEFINE(_SIGPROF, SIGPROF);
254 	DEFINE(_SIGXCPU, SIGXCPU);
255 	DEFINE(_SIGXFSZ, SIGXFSZ);
256 	BLANK();
257 }
258 
259 #ifdef CONFIG_SMP
260 void output_smpboot_defines(void)
261 {
262 	COMMENT("Linux smp cpu boot offsets.");
263 	OFFSET(CPU_BOOT_STACK, secondary_data, stack);
264 	OFFSET(CPU_BOOT_TINFO, secondary_data, thread_info);
265 	BLANK();
266 }
267 #endif
268 
269 #ifdef CONFIG_HIBERNATION
270 void output_pbe_defines(void)
271 {
272 	COMMENT("Linux struct pbe offsets.");
273 	OFFSET(PBE_ADDRESS, pbe, address);
274 	OFFSET(PBE_ORIG_ADDRESS, pbe, orig_address);
275 	OFFSET(PBE_NEXT, pbe, next);
276 	DEFINE(PBE_SIZE, sizeof(struct pbe));
277 	BLANK();
278 }
279 #endif
280 
281 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
282 void output_fgraph_ret_regs_defines(void)
283 {
284 	COMMENT("LoongArch fgraph_ret_regs offsets.");
285 	OFFSET(FGRET_REGS_A0, fgraph_ret_regs, regs[0]);
286 	OFFSET(FGRET_REGS_A1, fgraph_ret_regs, regs[1]);
287 	OFFSET(FGRET_REGS_FP, fgraph_ret_regs, fp);
288 	DEFINE(FGRET_REGS_SIZE, sizeof(struct fgraph_ret_regs));
289 	BLANK();
290 }
291 #endif
292