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