1 /* 2 * Test the framework itself. These tests confirm that setup works. 3 * 4 * Copyright (C) 2014, Red Hat Inc, Andrew Jones <drjones@redhat.com> 5 * 6 * This work is licensed under the terms of the GNU LGPL, version 2. 7 */ 8 #include <libcflat.h> 9 #include <alloc.h> 10 #include <devicetree.h> 11 #include <asm/setup.h> 12 #include <asm/ptrace.h> 13 #include <asm/asm-offsets.h> 14 #include <asm/processor.h> 15 #include <asm/thread_info.h> 16 #include <asm/psci.h> 17 #include <asm/smp.h> 18 #include <asm/cpumask.h> 19 #include <asm/barrier.h> 20 21 static void assert_args(int num_args, int needed_args) 22 { 23 if (num_args < needed_args) { 24 printf("selftest: not enough arguments\n"); 25 abort(); 26 } 27 } 28 29 static char *split_var(char *s, long *val) 30 { 31 char *p; 32 33 p = strchr(s, '='); 34 if (!p) 35 return NULL; 36 37 *val = atol(p+1); 38 *p = '\0'; 39 40 return s; 41 } 42 43 static void check_setup(int argc, char **argv) 44 { 45 int nr_tests = 0, i; 46 char *var; 47 long val; 48 49 for (i = 0; i < argc; ++i) { 50 51 var = split_var(argv[i], &val); 52 if (!var) 53 continue; 54 55 report_prefix_push(var); 56 57 if (strcmp(var, "mem") == 0) { 58 59 phys_addr_t memsize = PHYS_END - PHYS_OFFSET; 60 phys_addr_t expected = ((phys_addr_t)val)*1024*1024; 61 62 report("size = %d MB", memsize == expected, 63 memsize/1024/1024); 64 ++nr_tests; 65 66 } else if (strcmp(var, "smp") == 0) { 67 68 report("nr_cpus = %d", nr_cpus == (int)val, nr_cpus); 69 ++nr_tests; 70 } 71 72 report_prefix_pop(); 73 } 74 75 assert_args(nr_tests, 2); 76 } 77 78 static struct pt_regs expected_regs; 79 static bool und_works; 80 static bool svc_works; 81 #if defined(__arm__) 82 /* 83 * Capture the current register state and execute an instruction 84 * that causes an exception. The test handler will check that its 85 * capture of the current register state matches the capture done 86 * here. 87 * 88 * NOTE: update clobber list if passed insns needs more than r0,r1 89 */ 90 #define test_exception(pre_insns, excptn_insn, post_insns) \ 91 asm volatile( \ 92 pre_insns "\n" \ 93 "mov r0, %0\n" \ 94 "stmia r0, { r0-lr }\n" \ 95 "mrs r1, cpsr\n" \ 96 "str r1, [r0, #" xstr(S_PSR) "]\n" \ 97 "mov r1, #-1\n" \ 98 "str r1, [r0, #" xstr(S_OLD_R0) "]\n" \ 99 "add r1, pc, #8\n" \ 100 "str r1, [r0, #" xstr(S_R1) "]\n" \ 101 "str r1, [r0, #" xstr(S_PC) "]\n" \ 102 excptn_insn "\n" \ 103 post_insns "\n" \ 104 :: "r" (&expected_regs) : "r0", "r1") 105 106 static bool check_regs(struct pt_regs *regs) 107 { 108 unsigned i; 109 110 /* exception handlers should always run in svc mode */ 111 if (current_mode() != SVC_MODE) 112 return false; 113 114 for (i = 0; i < ARRAY_SIZE(regs->uregs); ++i) { 115 if (regs->uregs[i] != expected_regs.uregs[i]) 116 return false; 117 } 118 119 return true; 120 } 121 122 static void und_handler(struct pt_regs *regs) 123 { 124 und_works = check_regs(regs); 125 } 126 127 static bool check_und(void) 128 { 129 install_exception_handler(EXCPTN_UND, und_handler); 130 131 /* issue an instruction to a coprocessor we don't have */ 132 test_exception("", "mcr p2, 0, r0, c0, c0", ""); 133 134 install_exception_handler(EXCPTN_UND, NULL); 135 136 return und_works; 137 } 138 139 static void svc_handler(struct pt_regs *regs) 140 { 141 u32 svc = *(u32 *)(regs->ARM_pc - 4) & 0xffffff; 142 143 if (processor_mode(regs) == SVC_MODE) { 144 /* 145 * When issuing an svc from supervisor mode lr_svc will 146 * get corrupted. So before issuing the svc, callers must 147 * always push it on the stack. We pushed it to offset 4. 148 */ 149 regs->ARM_lr = *(unsigned long *)(regs->ARM_sp + 4); 150 } 151 152 svc_works = check_regs(regs) && svc == 123; 153 } 154 155 static bool check_svc(void) 156 { 157 install_exception_handler(EXCPTN_SVC, svc_handler); 158 159 if (current_mode() == SVC_MODE) { 160 /* 161 * An svc from supervisor mode will corrupt lr_svc and 162 * spsr_svc. We need to save/restore them separately. 163 */ 164 test_exception( 165 "mrs r0, spsr\n" 166 "push { r0,lr }\n", 167 "svc #123\n", 168 "pop { r0,lr }\n" 169 "msr spsr_cxsf, r0\n" 170 ); 171 } else { 172 test_exception("", "svc #123", ""); 173 } 174 175 install_exception_handler(EXCPTN_SVC, NULL); 176 177 return svc_works; 178 } 179 #elif defined(__aarch64__) 180 181 /* 182 * Capture the current register state and execute an instruction 183 * that causes an exception. The test handler will check that its 184 * capture of the current register state matches the capture done 185 * here. 186 * 187 * NOTE: update clobber list if passed insns needs more than x0,x1 188 */ 189 #define test_exception(pre_insns, excptn_insn, post_insns) \ 190 asm volatile( \ 191 pre_insns "\n" \ 192 "mov x1, %0\n" \ 193 "ldr x0, [x1, #" xstr(S_PSTATE) "]\n" \ 194 "mrs x1, nzcv\n" \ 195 "orr w0, w0, w1\n" \ 196 "mov x1, %0\n" \ 197 "str w0, [x1, #" xstr(S_PSTATE) "]\n" \ 198 "mov x0, sp\n" \ 199 "str x0, [x1, #" xstr(S_SP) "]\n" \ 200 "adr x0, 1f\n" \ 201 "str x0, [x1, #" xstr(S_PC) "]\n" \ 202 "stp x2, x3, [x1, #16]\n" \ 203 "stp x4, x5, [x1, #32]\n" \ 204 "stp x6, x7, [x1, #48]\n" \ 205 "stp x8, x9, [x1, #64]\n" \ 206 "stp x10, x11, [x1, #80]\n" \ 207 "stp x12, x13, [x1, #96]\n" \ 208 "stp x14, x15, [x1, #112]\n" \ 209 "stp x16, x17, [x1, #128]\n" \ 210 "stp x18, x19, [x1, #144]\n" \ 211 "stp x20, x21, [x1, #160]\n" \ 212 "stp x22, x23, [x1, #176]\n" \ 213 "stp x24, x25, [x1, #192]\n" \ 214 "stp x26, x27, [x1, #208]\n" \ 215 "stp x28, x29, [x1, #224]\n" \ 216 "str x30, [x1, #" xstr(S_LR) "]\n" \ 217 "stp x0, x1, [x1]\n" \ 218 "1:" excptn_insn "\n" \ 219 post_insns "\n" \ 220 :: "r" (&expected_regs) : "x0", "x1") 221 222 static bool check_regs(struct pt_regs *regs) 223 { 224 unsigned i; 225 226 /* exception handlers should always run in EL1 */ 227 if (current_level() != CurrentEL_EL1) 228 return false; 229 230 for (i = 0; i < ARRAY_SIZE(regs->regs); ++i) { 231 if (regs->regs[i] != expected_regs.regs[i]) 232 return false; 233 } 234 235 regs->pstate &= 0xf0000000 /* NZCV */ | 0x3c0 /* DAIF */ 236 | PSR_MODE_MASK; 237 238 return regs->sp == expected_regs.sp 239 && regs->pc == expected_regs.pc 240 && regs->pstate == expected_regs.pstate; 241 } 242 243 static enum vector check_vector_prep(void) 244 { 245 unsigned long daif; 246 247 if (is_user()) 248 return EL0_SYNC_64; 249 250 asm volatile("mrs %0, daif" : "=r" (daif) ::); 251 expected_regs.pstate = daif | PSR_MODE_EL1h; 252 return EL1H_SYNC; 253 } 254 255 static void unknown_handler(struct pt_regs *regs, unsigned int esr __unused) 256 { 257 und_works = check_regs(regs); 258 regs->pc += 4; 259 } 260 261 static bool check_und(void) 262 { 263 enum vector v = check_vector_prep(); 264 265 install_exception_handler(v, ESR_EL1_EC_UNKNOWN, unknown_handler); 266 267 /* try to read an el2 sysreg from el0/1 */ 268 test_exception("", "mrs x0, sctlr_el2", ""); 269 270 install_exception_handler(v, ESR_EL1_EC_UNKNOWN, NULL); 271 272 return und_works; 273 } 274 275 static void svc_handler(struct pt_regs *regs, unsigned int esr) 276 { 277 u16 svc = esr & 0xffff; 278 279 expected_regs.pc += 4; 280 svc_works = check_regs(regs) && svc == 123; 281 } 282 283 static bool check_svc(void) 284 { 285 enum vector v = check_vector_prep(); 286 287 install_exception_handler(v, ESR_EL1_EC_SVC64, svc_handler); 288 289 test_exception("", "svc #123", ""); 290 291 install_exception_handler(v, ESR_EL1_EC_SVC64, NULL); 292 293 return svc_works; 294 } 295 #endif 296 297 static void check_vectors(void *arg __unused) 298 { 299 report("und", check_und()); 300 report("svc", check_svc()); 301 exit(report_summary()); 302 } 303 304 static bool psci_check(void) 305 { 306 const struct fdt_property *method; 307 int node, len, ver; 308 309 node = fdt_node_offset_by_compatible(dt_fdt(), -1, "arm,psci-0.2"); 310 if (node < 0) { 311 printf("PSCI v0.2 compatibility required\n"); 312 return false; 313 } 314 315 method = fdt_get_property(dt_fdt(), node, "method", &len); 316 if (method == NULL) { 317 printf("bad psci device tree node\n"); 318 return false; 319 } 320 321 if (len < 4 || strcmp(method->data, "hvc") != 0) { 322 printf("psci method must be hvc\n"); 323 return false; 324 } 325 326 ver = psci_invoke(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0); 327 printf("PSCI version %d.%d\n", PSCI_VERSION_MAJOR(ver), 328 PSCI_VERSION_MINOR(ver)); 329 330 return true; 331 } 332 333 static cpumask_t smp_reported; 334 static void cpu_report(void) 335 { 336 int cpu = smp_processor_id(); 337 338 report("CPU%d online", true, cpu); 339 cpumask_set_cpu(cpu, &smp_reported); 340 halt(); 341 } 342 343 int main(int argc, char **argv) 344 { 345 report_prefix_push("selftest"); 346 assert_args(argc, 1); 347 report_prefix_push(argv[0]); 348 349 if (strcmp(argv[0], "setup") == 0) { 350 351 check_setup(argc-1, &argv[1]); 352 353 } else if (strcmp(argv[0], "vectors-kernel") == 0) { 354 355 check_vectors(NULL); 356 357 } else if (strcmp(argv[0], "vectors-user") == 0) { 358 359 void *sp = memalign(THREAD_SIZE, THREAD_SIZE); 360 start_usr(check_vectors, NULL, 361 (unsigned long)sp + THREAD_START_SP); 362 363 } else if (strcmp(argv[0], "smp") == 0) { 364 365 int cpu; 366 367 report("PSCI version", psci_check()); 368 369 for_each_present_cpu(cpu) { 370 if (cpu == 0) 371 continue; 372 smp_boot_secondary(cpu, cpu_report); 373 } 374 375 cpumask_set_cpu(0, &smp_reported); 376 while (!cpumask_full(&smp_reported)) 377 cpu_relax(); 378 } else { 379 printf("Unknown subtest\n"); 380 abort(); 381 } 382 383 return report_summary(); 384 } 385