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