err_inject.txt 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. IPF Machine Check (MC) error inject tool
  2. ========================================
  3. IPF Machine Check (MC) error inject tool is used to inject MC
  4. errors from Linux. The tool is a test bed for IPF MC work flow including
  5. hardware correctable error handling, OS recoverable error handling, MC
  6. event logging, etc.
  7. The tool includes two parts: a kernel driver and a user application
  8. sample. The driver provides interface to PAL to inject error
  9. and query error injection capabilities. The driver code is in
  10. arch/ia64/kernel/err_inject.c. The application sample (shown below)
  11. provides a combination of various errors and calls the driver's interface
  12. (sysfs interface) to inject errors or query error injection capabilities.
  13. The tool can be used to test Intel IPF machine MC handling capabilities.
  14. It's especially useful for people who can not access hardware MC injection
  15. tool to inject error. It's also very useful to integrate with other
  16. software test suits to do stressful testing on IPF.
  17. Below is a sample application as part of the whole tool. The sample
  18. can be used as a working test tool. Or it can be expanded to include
  19. more features. It also can be a integrated into a library or other user
  20. application to have more thorough test.
  21. The sample application takes err.conf as error configuration input. GCC
  22. compiles the code. After you install err_inject driver, you can run
  23. this sample application to inject errors.
  24. Errata: Itanium 2 Processors Specification Update lists some errata against
  25. the pal_mc_error_inject PAL procedure. The following err.conf has been tested
  26. on latest Montecito PAL.
  27. err.conf:
  28. #This is configuration file for err_inject_tool.
  29. #The format of the each line is:
  30. #cpu, loop, interval, err_type_info, err_struct_info, err_data_buffer
  31. #where
  32. # cpu: logical cpu number the error will be inject in.
  33. # loop: times the error will be injected.
  34. # interval: In second. every so often one error is injected.
  35. # err_type_info, err_struct_info: PAL parameters.
  36. #
  37. #Note: All values are hex w/o or w/ 0x prefix.
  38. #On cpu2, inject only total 0x10 errors, interval 5 seconds
  39. #corrected, data cache, hier-2, physical addr(assigned by tool code).
  40. #working on Montecito latest PAL.
  41. 2, 10, 5, 4101, 95
  42. #On cpu4, inject and consume total 0x10 errors, interval 5 seconds
  43. #corrected, data cache, hier-2, physical addr(assigned by tool code).
  44. #working on Montecito latest PAL.
  45. 4, 10, 5, 4109, 95
  46. #On cpu15, inject and consume total 0x10 errors, interval 5 seconds
  47. #recoverable, DTR0, hier-2.
  48. #working on Montecito latest PAL.
  49. 0xf, 0x10, 5, 4249, 15
  50. The sample application source code:
  51. err_injection_tool.c:
  52. /*
  53. * This program is free software; you can redistribute it and/or modify
  54. * it under the terms of the GNU General Public License as published by
  55. * the Free Software Foundation; either version 2 of the License, or
  56. * (at your option) any later version.
  57. *
  58. * This program is distributed in the hope that it will be useful, but
  59. * WITHOUT ANY WARRANTY; without even the implied warranty of
  60. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  61. * NON INFRINGEMENT. See the GNU General Public License for more
  62. * details.
  63. *
  64. * You should have received a copy of the GNU General Public License
  65. * along with this program; if not, write to the Free Software
  66. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  67. *
  68. * Copyright (C) 2006 Intel Co
  69. * Fenghua Yu <fenghua.yu@intel.com>
  70. *
  71. */
  72. #include <sys/types.h>
  73. #include <sys/stat.h>
  74. #include <fcntl.h>
  75. #include <stdio.h>
  76. #include <sched.h>
  77. #include <unistd.h>
  78. #include <stdlib.h>
  79. #include <stdarg.h>
  80. #include <string.h>
  81. #include <errno.h>
  82. #include <time.h>
  83. #include <sys/ipc.h>
  84. #include <sys/sem.h>
  85. #include <sys/wait.h>
  86. #include <sys/mman.h>
  87. #include <sys/shm.h>
  88. #define MAX_FN_SIZE 256
  89. #define MAX_BUF_SIZE 256
  90. #define DATA_BUF_SIZE 256
  91. #define NR_CPUS 512
  92. #define MAX_TASK_NUM 2048
  93. #define MIN_INTERVAL 5 // seconds
  94. #define ERR_DATA_BUFFER_SIZE 3 // Three 8-byte.
  95. #define PARA_FIELD_NUM 5
  96. #define MASK_SIZE (NR_CPUS/64)
  97. #define PATH_FORMAT "/sys/devices/system/cpu/cpu%d/err_inject/"
  98. int sched_setaffinity(pid_t pid, unsigned int len, unsigned long *mask);
  99. int verbose;
  100. #define vbprintf if (verbose) printf
  101. int log_info(int cpu, const char *fmt, ...)
  102. {
  103. FILE *log;
  104. char fn[MAX_FN_SIZE];
  105. char buf[MAX_BUF_SIZE];
  106. va_list args;
  107. sprintf(fn, "%d.log", cpu);
  108. log=fopen(fn, "a+");
  109. if (log==NULL) {
  110. perror("Error open:");
  111. return -1;
  112. }
  113. va_start(args, fmt);
  114. vprintf(fmt, args);
  115. memset(buf, 0, MAX_BUF_SIZE);
  116. vsprintf(buf, fmt, args);
  117. va_end(args);
  118. fwrite(buf, sizeof(buf), 1, log);
  119. fclose(log);
  120. return 0;
  121. }
  122. typedef unsigned long u64;
  123. typedef unsigned int u32;
  124. typedef union err_type_info_u {
  125. struct {
  126. u64 mode : 3, /* 0-2 */
  127. err_inj : 3, /* 3-5 */
  128. err_sev : 2, /* 6-7 */
  129. err_struct : 5, /* 8-12 */
  130. struct_hier : 3, /* 13-15 */
  131. reserved : 48; /* 16-63 */
  132. } err_type_info_u;
  133. u64 err_type_info;
  134. } err_type_info_t;
  135. typedef union err_struct_info_u {
  136. struct {
  137. u64 siv : 1, /* 0 */
  138. c_t : 2, /* 1-2 */
  139. cl_p : 3, /* 3-5 */
  140. cl_id : 3, /* 6-8 */
  141. cl_dp : 1, /* 9 */
  142. reserved1 : 22, /* 10-31 */
  143. tiv : 1, /* 32 */
  144. trigger : 4, /* 33-36 */
  145. trigger_pl : 3, /* 37-39 */
  146. reserved2 : 24; /* 40-63 */
  147. } err_struct_info_cache;
  148. struct {
  149. u64 siv : 1, /* 0 */
  150. tt : 2, /* 1-2 */
  151. tc_tr : 2, /* 3-4 */
  152. tr_slot : 8, /* 5-12 */
  153. reserved1 : 19, /* 13-31 */
  154. tiv : 1, /* 32 */
  155. trigger : 4, /* 33-36 */
  156. trigger_pl : 3, /* 37-39 */
  157. reserved2 : 24; /* 40-63 */
  158. } err_struct_info_tlb;
  159. struct {
  160. u64 siv : 1, /* 0 */
  161. regfile_id : 4, /* 1-4 */
  162. reg_num : 7, /* 5-11 */
  163. reserved1 : 20, /* 12-31 */
  164. tiv : 1, /* 32 */
  165. trigger : 4, /* 33-36 */
  166. trigger_pl : 3, /* 37-39 */
  167. reserved2 : 24; /* 40-63 */
  168. } err_struct_info_register;
  169. struct {
  170. u64 reserved;
  171. } err_struct_info_bus_processor_interconnect;
  172. u64 err_struct_info;
  173. } err_struct_info_t;
  174. typedef union err_data_buffer_u {
  175. struct {
  176. u64 trigger_addr; /* 0-63 */
  177. u64 inj_addr; /* 64-127 */
  178. u64 way : 5, /* 128-132 */
  179. index : 20, /* 133-152 */
  180. : 39; /* 153-191 */
  181. } err_data_buffer_cache;
  182. struct {
  183. u64 trigger_addr; /* 0-63 */
  184. u64 inj_addr; /* 64-127 */
  185. u64 way : 5, /* 128-132 */
  186. index : 20, /* 133-152 */
  187. reserved : 39; /* 153-191 */
  188. } err_data_buffer_tlb;
  189. struct {
  190. u64 trigger_addr; /* 0-63 */
  191. } err_data_buffer_register;
  192. struct {
  193. u64 reserved; /* 0-63 */
  194. } err_data_buffer_bus_processor_interconnect;
  195. u64 err_data_buffer[ERR_DATA_BUFFER_SIZE];
  196. } err_data_buffer_t;
  197. typedef union capabilities_u {
  198. struct {
  199. u64 i : 1,
  200. d : 1,
  201. rv : 1,
  202. tag : 1,
  203. data : 1,
  204. mesi : 1,
  205. dp : 1,
  206. reserved1 : 3,
  207. pa : 1,
  208. va : 1,
  209. wi : 1,
  210. reserved2 : 20,
  211. trigger : 1,
  212. trigger_pl : 1,
  213. reserved3 : 30;
  214. } capabilities_cache;
  215. struct {
  216. u64 d : 1,
  217. i : 1,
  218. rv : 1,
  219. tc : 1,
  220. tr : 1,
  221. reserved1 : 27,
  222. trigger : 1,
  223. trigger_pl : 1,
  224. reserved2 : 30;
  225. } capabilities_tlb;
  226. struct {
  227. u64 gr_b0 : 1,
  228. gr_b1 : 1,
  229. fr : 1,
  230. br : 1,
  231. pr : 1,
  232. ar : 1,
  233. cr : 1,
  234. rr : 1,
  235. pkr : 1,
  236. dbr : 1,
  237. ibr : 1,
  238. pmc : 1,
  239. pmd : 1,
  240. reserved1 : 3,
  241. regnum : 1,
  242. reserved2 : 15,
  243. trigger : 1,
  244. trigger_pl : 1,
  245. reserved3 : 30;
  246. } capabilities_register;
  247. struct {
  248. u64 reserved;
  249. } capabilities_bus_processor_interconnect;
  250. } capabilities_t;
  251. typedef struct resources_s {
  252. u64 ibr0 : 1,
  253. ibr2 : 1,
  254. ibr4 : 1,
  255. ibr6 : 1,
  256. dbr0 : 1,
  257. dbr2 : 1,
  258. dbr4 : 1,
  259. dbr6 : 1,
  260. reserved : 48;
  261. } resources_t;
  262. long get_page_size(void)
  263. {
  264. long page_size=sysconf(_SC_PAGESIZE);
  265. return page_size;
  266. }
  267. #define PAGE_SIZE (get_page_size()==-1?0x4000:get_page_size())
  268. #define SHM_SIZE (2*PAGE_SIZE*NR_CPUS)
  269. #define SHM_VA 0x2000000100000000
  270. int shmid;
  271. void *shmaddr;
  272. int create_shm(void)
  273. {
  274. key_t key;
  275. char fn[MAX_FN_SIZE];
  276. /* cpu0 is always existing */
  277. sprintf(fn, PATH_FORMAT, 0);
  278. if ((key = ftok(fn, 's')) == -1) {
  279. perror("ftok");
  280. return -1;
  281. }
  282. shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT);
  283. if (shmid == -1) {
  284. if (errno==EEXIST) {
  285. shmid = shmget(key, SHM_SIZE, 0);
  286. if (shmid == -1) {
  287. perror("shmget");
  288. return -1;
  289. }
  290. }
  291. else {
  292. perror("shmget");
  293. return -1;
  294. }
  295. }
  296. vbprintf("shmid=%d", shmid);
  297. /* connect to the segment: */
  298. shmaddr = shmat(shmid, (void *)SHM_VA, 0);
  299. if (shmaddr == (void*)-1) {
  300. perror("shmat");
  301. return -1;
  302. }
  303. memset(shmaddr, 0, SHM_SIZE);
  304. mlock(shmaddr, SHM_SIZE);
  305. return 0;
  306. }
  307. int free_shm()
  308. {
  309. munlock(shmaddr, SHM_SIZE);
  310. shmdt(shmaddr);
  311. semctl(shmid, 0, IPC_RMID);
  312. return 0;
  313. }
  314. #ifdef _SEM_SEMUN_UNDEFINED
  315. union semun
  316. {
  317. int val;
  318. struct semid_ds *buf;
  319. unsigned short int *array;
  320. struct seminfo *__buf;
  321. };
  322. #endif
  323. u32 mode=1; /* 1: physical mode; 2: virtual mode. */
  324. int one_lock=1;
  325. key_t key[NR_CPUS];
  326. int semid[NR_CPUS];
  327. int create_sem(int cpu)
  328. {
  329. union semun arg;
  330. char fn[MAX_FN_SIZE];
  331. int sid;
  332. sprintf(fn, PATH_FORMAT, cpu);
  333. sprintf(fn, "%s/%s", fn, "err_type_info");
  334. if ((key[cpu] = ftok(fn, 'e')) == -1) {
  335. perror("ftok");
  336. return -1;
  337. }
  338. if (semid[cpu]!=0)
  339. return 0;
  340. /* clear old semaphore */
  341. if ((sid = semget(key[cpu], 1, 0)) != -1)
  342. semctl(sid, 0, IPC_RMID);
  343. /* get one semaphore */
  344. if ((semid[cpu] = semget(key[cpu], 1, IPC_CREAT | IPC_EXCL)) == -1) {
  345. perror("semget");
  346. printf("Please remove semaphore with key=0x%lx, then run the tool.\n",
  347. (u64)key[cpu]);
  348. return -1;
  349. }
  350. vbprintf("semid[%d]=0x%lx, key[%d]=%lx\n",cpu,(u64)semid[cpu],cpu,
  351. (u64)key[cpu]);
  352. /* initialize the semaphore to 1: */
  353. arg.val = 1;
  354. if (semctl(semid[cpu], 0, SETVAL, arg) == -1) {
  355. perror("semctl");
  356. return -1;
  357. }
  358. return 0;
  359. }
  360. static int lock(int cpu)
  361. {
  362. struct sembuf lock;
  363. lock.sem_num = cpu;
  364. lock.sem_op = 1;
  365. semop(semid[cpu], &lock, 1);
  366. return 0;
  367. }
  368. static int unlock(int cpu)
  369. {
  370. struct sembuf unlock;
  371. unlock.sem_num = cpu;
  372. unlock.sem_op = -1;
  373. semop(semid[cpu], &unlock, 1);
  374. return 0;
  375. }
  376. void free_sem(int cpu)
  377. {
  378. semctl(semid[cpu], 0, IPC_RMID);
  379. }
  380. int wr_multi(char *fn, unsigned long *data, int size)
  381. {
  382. int fd;
  383. char buf[MAX_BUF_SIZE];
  384. int ret;
  385. if (size==1)
  386. sprintf(buf, "%lx", *data);
  387. else if (size==3)
  388. sprintf(buf, "%lx,%lx,%lx", data[0], data[1], data[2]);
  389. else {
  390. fprintf(stderr,"write to file with wrong size!\n");
  391. return -1;
  392. }
  393. fd=open(fn, O_RDWR);
  394. if (!fd) {
  395. perror("Error:");
  396. return -1;
  397. }
  398. ret=write(fd, buf, sizeof(buf));
  399. close(fd);
  400. return ret;
  401. }
  402. int wr(char *fn, unsigned long data)
  403. {
  404. return wr_multi(fn, &data, 1);
  405. }
  406. int rd(char *fn, unsigned long *data)
  407. {
  408. int fd;
  409. char buf[MAX_BUF_SIZE];
  410. fd=open(fn, O_RDONLY);
  411. if (fd<0) {
  412. perror("Error:");
  413. return -1;
  414. }
  415. read(fd, buf, MAX_BUF_SIZE);
  416. *data=strtoul(buf, NULL, 16);
  417. close(fd);
  418. return 0;
  419. }
  420. int rd_status(char *path, int *status)
  421. {
  422. char fn[MAX_FN_SIZE];
  423. sprintf(fn, "%s/status", path);
  424. if (rd(fn, (u64*)status)<0) {
  425. perror("status reading error.\n");
  426. return -1;
  427. }
  428. return 0;
  429. }
  430. int rd_capabilities(char *path, u64 *capabilities)
  431. {
  432. char fn[MAX_FN_SIZE];
  433. sprintf(fn, "%s/capabilities", path);
  434. if (rd(fn, capabilities)<0) {
  435. perror("capabilities reading error.\n");
  436. return -1;
  437. }
  438. return 0;
  439. }
  440. int rd_all(char *path)
  441. {
  442. unsigned long err_type_info, err_struct_info, err_data_buffer;
  443. int status;
  444. unsigned long capabilities, resources;
  445. char fn[MAX_FN_SIZE];
  446. sprintf(fn, "%s/err_type_info", path);
  447. if (rd(fn, &err_type_info)<0) {
  448. perror("err_type_info reading error.\n");
  449. return -1;
  450. }
  451. printf("err_type_info=%lx\n", err_type_info);
  452. sprintf(fn, "%s/err_struct_info", path);
  453. if (rd(fn, &err_struct_info)<0) {
  454. perror("err_struct_info reading error.\n");
  455. return -1;
  456. }
  457. printf("err_struct_info=%lx\n", err_struct_info);
  458. sprintf(fn, "%s/err_data_buffer", path);
  459. if (rd(fn, &err_data_buffer)<0) {
  460. perror("err_data_buffer reading error.\n");
  461. return -1;
  462. }
  463. printf("err_data_buffer=%lx\n", err_data_buffer);
  464. sprintf(fn, "%s/status", path);
  465. if (rd("status", (u64*)&status)<0) {
  466. perror("status reading error.\n");
  467. return -1;
  468. }
  469. printf("status=%d\n", status);
  470. sprintf(fn, "%s/capabilities", path);
  471. if (rd(fn,&capabilities)<0) {
  472. perror("capabilities reading error.\n");
  473. return -1;
  474. }
  475. printf("capabilities=%lx\n", capabilities);
  476. sprintf(fn, "%s/resources", path);
  477. if (rd(fn, &resources)<0) {
  478. perror("resources reading error.\n");
  479. return -1;
  480. }
  481. printf("resources=%lx\n", resources);
  482. return 0;
  483. }
  484. int query_capabilities(char *path, err_type_info_t err_type_info,
  485. u64 *capabilities)
  486. {
  487. char fn[MAX_FN_SIZE];
  488. err_struct_info_t err_struct_info;
  489. err_data_buffer_t err_data_buffer;
  490. err_struct_info.err_struct_info=0;
  491. memset(err_data_buffer.err_data_buffer, -1, ERR_DATA_BUFFER_SIZE*8);
  492. sprintf(fn, "%s/err_type_info", path);
  493. wr(fn, err_type_info.err_type_info);
  494. sprintf(fn, "%s/err_struct_info", path);
  495. wr(fn, 0x0);
  496. sprintf(fn, "%s/err_data_buffer", path);
  497. wr_multi(fn, err_data_buffer.err_data_buffer, ERR_DATA_BUFFER_SIZE);
  498. // Fire pal_mc_error_inject procedure.
  499. sprintf(fn, "%s/call_start", path);
  500. wr(fn, mode);
  501. if (rd_capabilities(path, capabilities)<0)
  502. return -1;
  503. return 0;
  504. }
  505. int query_all_capabilities()
  506. {
  507. int status;
  508. err_type_info_t err_type_info;
  509. int err_sev, err_struct, struct_hier;
  510. int cap=0;
  511. u64 capabilities;
  512. char path[MAX_FN_SIZE];
  513. err_type_info.err_type_info=0; // Initial
  514. err_type_info.err_type_info_u.mode=0; // Query mode;
  515. err_type_info.err_type_info_u.err_inj=0;
  516. printf("All capabilities implemented in pal_mc_error_inject:\n");
  517. sprintf(path, PATH_FORMAT ,0);
  518. for (err_sev=0;err_sev<3;err_sev++)
  519. for (err_struct=0;err_struct<5;err_struct++)
  520. for (struct_hier=0;struct_hier<5;struct_hier++)
  521. {
  522. status=-1;
  523. capabilities=0;
  524. err_type_info.err_type_info_u.err_sev=err_sev;
  525. err_type_info.err_type_info_u.err_struct=err_struct;
  526. err_type_info.err_type_info_u.struct_hier=struct_hier;
  527. if (query_capabilities(path, err_type_info, &capabilities)<0)
  528. continue;
  529. if (rd_status(path, &status)<0)
  530. continue;
  531. if (status==0) {
  532. cap=1;
  533. printf("For err_sev=%d, err_struct=%d, struct_hier=%d: ",
  534. err_sev, err_struct, struct_hier);
  535. printf("capabilities 0x%lx\n", capabilities);
  536. }
  537. }
  538. if (!cap) {
  539. printf("No capabilities supported.\n");
  540. return 0;
  541. }
  542. return 0;
  543. }
  544. int err_inject(int cpu, char *path, err_type_info_t err_type_info,
  545. err_struct_info_t err_struct_info,
  546. err_data_buffer_t err_data_buffer)
  547. {
  548. int status;
  549. char fn[MAX_FN_SIZE];
  550. log_info(cpu, "err_type_info=%lx, err_struct_info=%lx, ",
  551. err_type_info.err_type_info,
  552. err_struct_info.err_struct_info);
  553. log_info(cpu,"err_data_buffer=[%lx,%lx,%lx]\n",
  554. err_data_buffer.err_data_buffer[0],
  555. err_data_buffer.err_data_buffer[1],
  556. err_data_buffer.err_data_buffer[2]);
  557. sprintf(fn, "%s/err_type_info", path);
  558. wr(fn, err_type_info.err_type_info);
  559. sprintf(fn, "%s/err_struct_info", path);
  560. wr(fn, err_struct_info.err_struct_info);
  561. sprintf(fn, "%s/err_data_buffer", path);
  562. wr_multi(fn, err_data_buffer.err_data_buffer, ERR_DATA_BUFFER_SIZE);
  563. // Fire pal_mc_error_inject procedure.
  564. sprintf(fn, "%s/call_start", path);
  565. wr(fn,mode);
  566. if (rd_status(path, &status)<0) {
  567. vbprintf("fail: read status\n");
  568. return -100;
  569. }
  570. if (status!=0) {
  571. log_info(cpu, "fail: status=%d\n", status);
  572. return status;
  573. }
  574. return status;
  575. }
  576. static int construct_data_buf(char *path, err_type_info_t err_type_info,
  577. err_struct_info_t err_struct_info,
  578. err_data_buffer_t *err_data_buffer,
  579. void *va1)
  580. {
  581. char fn[MAX_FN_SIZE];
  582. u64 virt_addr=0, phys_addr=0;
  583. vbprintf("va1=%lx\n", (u64)va1);
  584. memset(&err_data_buffer->err_data_buffer_cache, 0, ERR_DATA_BUFFER_SIZE*8);
  585. switch (err_type_info.err_type_info_u.err_struct) {
  586. case 1: // Cache
  587. switch (err_struct_info.err_struct_info_cache.cl_id) {
  588. case 1: //Virtual addr
  589. err_data_buffer->err_data_buffer_cache.inj_addr=(u64)va1;
  590. break;
  591. case 2: //Phys addr
  592. sprintf(fn, "%s/virtual_to_phys", path);
  593. virt_addr=(u64)va1;
  594. if (wr(fn,virt_addr)<0)
  595. return -1;
  596. rd(fn, &phys_addr);
  597. err_data_buffer->err_data_buffer_cache.inj_addr=phys_addr;
  598. break;
  599. default:
  600. printf("Not supported cl_id\n");
  601. break;
  602. }
  603. break;
  604. case 2: // TLB
  605. break;
  606. case 3: // Register file
  607. break;
  608. case 4: // Bus/system interconnect
  609. default:
  610. printf("Not supported err_struct\n");
  611. break;
  612. }
  613. return 0;
  614. }
  615. typedef struct {
  616. u64 cpu;
  617. u64 loop;
  618. u64 interval;
  619. u64 err_type_info;
  620. u64 err_struct_info;
  621. u64 err_data_buffer[ERR_DATA_BUFFER_SIZE];
  622. } parameters_t;
  623. parameters_t line_para;
  624. int para;
  625. static int empty_data_buffer(u64 *err_data_buffer)
  626. {
  627. int empty=1;
  628. int i;
  629. for (i=0;i<ERR_DATA_BUFFER_SIZE; i++)
  630. if (err_data_buffer[i]!=-1)
  631. empty=0;
  632. return empty;
  633. }
  634. int err_inj()
  635. {
  636. err_type_info_t err_type_info;
  637. err_struct_info_t err_struct_info;
  638. err_data_buffer_t err_data_buffer;
  639. int count;
  640. FILE *fp;
  641. unsigned long cpu, loop, interval, err_type_info_conf, err_struct_info_conf;
  642. u64 err_data_buffer_conf[ERR_DATA_BUFFER_SIZE];
  643. int num;
  644. int i;
  645. char path[MAX_FN_SIZE];
  646. parameters_t parameters[MAX_TASK_NUM]={};
  647. pid_t child_pid[MAX_TASK_NUM];
  648. time_t current_time;
  649. int status;
  650. if (!para) {
  651. fp=fopen("err.conf", "r");
  652. if (fp==NULL) {
  653. perror("Error open err.conf");
  654. return -1;
  655. }
  656. num=0;
  657. while (!feof(fp)) {
  658. char buf[256];
  659. memset(buf,0,256);
  660. fgets(buf, 256, fp);
  661. count=sscanf(buf, "%lx, %lx, %lx, %lx, %lx, %lx, %lx, %lx\n",
  662. &cpu, &loop, &interval,&err_type_info_conf,
  663. &err_struct_info_conf,
  664. &err_data_buffer_conf[0],
  665. &err_data_buffer_conf[1],
  666. &err_data_buffer_conf[2]);
  667. if (count!=PARA_FIELD_NUM+3) {
  668. err_data_buffer_conf[0]=-1;
  669. err_data_buffer_conf[1]=-1;
  670. err_data_buffer_conf[2]=-1;
  671. count=sscanf(buf, "%lx, %lx, %lx, %lx, %lx\n",
  672. &cpu, &loop, &interval,&err_type_info_conf,
  673. &err_struct_info_conf);
  674. if (count!=PARA_FIELD_NUM)
  675. continue;
  676. }
  677. parameters[num].cpu=cpu;
  678. parameters[num].loop=loop;
  679. parameters[num].interval= interval>MIN_INTERVAL
  680. ?interval:MIN_INTERVAL;
  681. parameters[num].err_type_info=err_type_info_conf;
  682. parameters[num].err_struct_info=err_struct_info_conf;
  683. memcpy(parameters[num++].err_data_buffer,
  684. err_data_buffer_conf,ERR_DATA_BUFFER_SIZE*8) ;
  685. if (num>=MAX_TASK_NUM)
  686. break;
  687. }
  688. }
  689. else {
  690. parameters[0].cpu=line_para.cpu;
  691. parameters[0].loop=line_para.loop;
  692. parameters[0].interval= line_para.interval>MIN_INTERVAL
  693. ?line_para.interval:MIN_INTERVAL;
  694. parameters[0].err_type_info=line_para.err_type_info;
  695. parameters[0].err_struct_info=line_para.err_struct_info;
  696. memcpy(parameters[0].err_data_buffer,
  697. line_para.err_data_buffer,ERR_DATA_BUFFER_SIZE*8) ;
  698. num=1;
  699. }
  700. /* Create semaphore: If one_lock, one semaphore for all processors.
  701. Otherwise, one semaphore for each processor. */
  702. if (one_lock) {
  703. if (create_sem(0)) {
  704. printf("Can not create semaphore...exit\n");
  705. free_sem(0);
  706. return -1;
  707. }
  708. }
  709. else {
  710. for (i=0;i<num;i++) {
  711. if (create_sem(parameters[i].cpu)) {
  712. printf("Can not create semaphore for cpu%d...exit\n",i);
  713. free_sem(parameters[num].cpu);
  714. return -1;
  715. }
  716. }
  717. }
  718. /* Create a shm segment which will be used to inject/consume errors on.*/
  719. if (create_shm()==-1) {
  720. printf("Error to create shm...exit\n");
  721. return -1;
  722. }
  723. for (i=0;i<num;i++) {
  724. pid_t pid;
  725. current_time=time(NULL);
  726. log_info(parameters[i].cpu, "\nBegine at %s", ctime(&current_time));
  727. log_info(parameters[i].cpu, "Configurations:\n");
  728. log_info(parameters[i].cpu,"On cpu%ld: loop=%lx, interval=%lx(s)",
  729. parameters[i].cpu,
  730. parameters[i].loop,
  731. parameters[i].interval);
  732. log_info(parameters[i].cpu," err_type_info=%lx,err_struct_info=%lx\n",
  733. parameters[i].err_type_info,
  734. parameters[i].err_struct_info);
  735. sprintf(path, PATH_FORMAT, (int)parameters[i].cpu);
  736. err_type_info.err_type_info=parameters[i].err_type_info;
  737. err_struct_info.err_struct_info=parameters[i].err_struct_info;
  738. memcpy(err_data_buffer.err_data_buffer,
  739. parameters[i].err_data_buffer,
  740. ERR_DATA_BUFFER_SIZE*8);
  741. pid=fork();
  742. if (pid==0) {
  743. unsigned long mask[MASK_SIZE];
  744. int j, k;
  745. void *va1, *va2;
  746. /* Allocate two memory areas va1 and va2 in shm */
  747. va1=shmaddr+parameters[i].cpu*PAGE_SIZE;
  748. va2=shmaddr+parameters[i].cpu*PAGE_SIZE+PAGE_SIZE;
  749. vbprintf("va1=%lx, va2=%lx\n", (u64)va1, (u64)va2);
  750. memset(va1, 0x1, PAGE_SIZE);
  751. memset(va2, 0x2, PAGE_SIZE);
  752. if (empty_data_buffer(err_data_buffer.err_data_buffer))
  753. /* If not specified yet, construct data buffer
  754. * with va1
  755. */
  756. construct_data_buf(path, err_type_info,
  757. err_struct_info, &err_data_buffer,va1);
  758. for (j=0;j<MASK_SIZE;j++)
  759. mask[j]=0;
  760. cpu=parameters[i].cpu;
  761. k = cpu%64;
  762. j = cpu/64;
  763. mask[j] = 1UL << k;
  764. if (sched_setaffinity(0, MASK_SIZE*8, mask)==-1) {
  765. perror("Error sched_setaffinity:");
  766. return -1;
  767. }
  768. for (j=0; j<parameters[i].loop; j++) {
  769. log_info(parameters[i].cpu,"Injection ");
  770. log_info(parameters[i].cpu,"on cpu%ld: #%d/%ld ",
  771. parameters[i].cpu,j+1, parameters[i].loop);
  772. /* Hold the lock */
  773. if (one_lock)
  774. lock(0);
  775. else
  776. /* Hold lock on this cpu */
  777. lock(parameters[i].cpu);
  778. if ((status=err_inject(parameters[i].cpu,
  779. path, err_type_info,
  780. err_struct_info, err_data_buffer))
  781. ==0) {
  782. /* consume the error for "inject only"*/
  783. memcpy(va2, va1, PAGE_SIZE);
  784. memcpy(va1, va2, PAGE_SIZE);
  785. log_info(parameters[i].cpu,
  786. "successful\n");
  787. }
  788. else {
  789. log_info(parameters[i].cpu,"fail:");
  790. log_info(parameters[i].cpu,
  791. "status=%d\n", status);
  792. unlock(parameters[i].cpu);
  793. break;
  794. }
  795. if (one_lock)
  796. /* Release the lock */
  797. unlock(0);
  798. /* Release lock on this cpu */
  799. else
  800. unlock(parameters[i].cpu);
  801. if (j < parameters[i].loop-1)
  802. sleep(parameters[i].interval);
  803. }
  804. current_time=time(NULL);
  805. log_info(parameters[i].cpu, "Done at %s", ctime(&current_time));
  806. return 0;
  807. }
  808. else if (pid<0) {
  809. perror("Error fork:");
  810. continue;
  811. }
  812. child_pid[i]=pid;
  813. }
  814. for (i=0;i<num;i++)
  815. waitpid(child_pid[i], NULL, 0);
  816. if (one_lock)
  817. free_sem(0);
  818. else
  819. for (i=0;i<num;i++)
  820. free_sem(parameters[i].cpu);
  821. printf("All done.\n");
  822. return 0;
  823. }
  824. void help()
  825. {
  826. printf("err_inject_tool:\n");
  827. printf("\t-q: query all capabilities. default: off\n");
  828. printf("\t-m: procedure mode. 1: physical 2: virtual. default: 1\n");
  829. printf("\t-i: inject errors. default: off\n");
  830. printf("\t-l: one lock per cpu. default: one lock for all\n");
  831. printf("\t-e: error parameters:\n");
  832. printf("\t\tcpu,loop,interval,err_type_info,err_struct_info[,err_data_buffer[0],err_data_buffer[1],err_data_buffer[2]]\n");
  833. printf("\t\t cpu: logical cpu number the error will be inject in.\n");
  834. printf("\t\t loop: times the error will be injected.\n");
  835. printf("\t\t interval: In second. every so often one error is injected.\n");
  836. printf("\t\t err_type_info, err_struct_info: PAL parameters.\n");
  837. printf("\t\t err_data_buffer: PAL parameter. Optional. If not present,\n");
  838. printf("\t\t it's constructed by tool automatically. Be\n");
  839. printf("\t\t careful to provide err_data_buffer and make\n");
  840. printf("\t\t sure it's working with the environment.\n");
  841. printf("\t Note:no space between error parameters.\n");
  842. printf("\t default: Take error parameters from err.conf instead of command line.\n");
  843. printf("\t-v: verbose. default: off\n");
  844. printf("\t-h: help\n\n");
  845. printf("The tool will take err.conf file as ");
  846. printf("input to inject single or multiple errors ");
  847. printf("on one or multiple cpus in parallel.\n");
  848. }
  849. int main(int argc, char **argv)
  850. {
  851. char c;
  852. int do_err_inj=0;
  853. int do_query_all=0;
  854. int count;
  855. u32 m;
  856. /* Default one lock for all cpu's */
  857. one_lock=1;
  858. while ((c = getopt(argc, argv, "m:iqvhle:")) != EOF)
  859. switch (c) {
  860. case 'm': /* Procedure mode. 1: phys 2: virt */
  861. count=sscanf(optarg, "%x", &m);
  862. if (count!=1 || (m!=1 && m!=2)) {
  863. printf("Wrong mode number.\n");
  864. help();
  865. return -1;
  866. }
  867. mode=m;
  868. break;
  869. case 'i': /* Inject errors */
  870. do_err_inj=1;
  871. break;
  872. case 'q': /* Query */
  873. do_query_all=1;
  874. break;
  875. case 'v': /* Verbose */
  876. verbose=1;
  877. break;
  878. case 'l': /* One lock per cpu */
  879. one_lock=0;
  880. break;
  881. case 'e': /* error arguments */
  882. /* Take parameters:
  883. * #cpu, loop, interval, err_type_info, err_struct_info[, err_data_buffer]
  884. * err_data_buffer is optional. Recommend not to specify
  885. * err_data_buffer. Better to use tool to generate it.
  886. */
  887. count=sscanf(optarg,
  888. "%lx, %lx, %lx, %lx, %lx, %lx, %lx, %lx\n",
  889. &line_para.cpu,
  890. &line_para.loop,
  891. &line_para.interval,
  892. &line_para.err_type_info,
  893. &line_para.err_struct_info,
  894. &line_para.err_data_buffer[0],
  895. &line_para.err_data_buffer[1],
  896. &line_para.err_data_buffer[2]);
  897. if (count!=PARA_FIELD_NUM+3) {
  898. line_para.err_data_buffer[0]=-1,
  899. line_para.err_data_buffer[1]=-1,
  900. line_para.err_data_buffer[2]=-1;
  901. count=sscanf(optarg, "%lx, %lx, %lx, %lx, %lx\n",
  902. &line_para.cpu,
  903. &line_para.loop,
  904. &line_para.interval,
  905. &line_para.err_type_info,
  906. &line_para.err_struct_info);
  907. if (count!=PARA_FIELD_NUM) {
  908. printf("Wrong error arguments.\n");
  909. help();
  910. return -1;
  911. }
  912. }
  913. para=1;
  914. break;
  915. continue;
  916. break;
  917. case 'h':
  918. help();
  919. return 0;
  920. default:
  921. break;
  922. }
  923. if (do_query_all)
  924. query_all_capabilities();
  925. if (do_err_inj)
  926. err_inj();
  927. if (!do_query_all && !do_err_inj)
  928. help();
  929. return 0;
  930. }