tcc.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Test if our calling convention gymnastics actually work
  3. */
  4. #include <efi.h>
  5. #include <efilib.h>
  6. #ifdef __x86_64__
  7. #include <x86_64/pe.h>
  8. #include <x86_64/efibind.h>
  9. #endif
  10. #if 0
  11. asm volatile("out %0,%1" : : "a" ((uint8_t)a), "dN" (0x80));
  12. extern void dump_stack(void);
  13. asm( ".globl dump_stack\n"
  14. "dump_stack:\n"
  15. " movq %rsp, %rdi\n"
  16. " jmp *dump_stack_helper@GOTPCREL(%rip)\n"
  17. ".size dump_stack, .-dump_stack");
  18. void dump_stack_helper(uint64_t rsp_val)
  19. {
  20. uint64_t *rsp = (uint64_t *)rsp_val;
  21. int x;
  22. Print(L"%%rsp: 0x%08x%08x stack:\r\n",
  23. (rsp_val & 0xffffffff00000000) >>32,
  24. rsp_val & 0xffffffff);
  25. for (x = 0; x < 8; x++) {
  26. Print(L"%08x: ", ((uint64_t)rsp) & 0xffffffff);
  27. Print(L"%016x ", *rsp++);
  28. Print(L"%016x ", *rsp++);
  29. Print(L"%016x ", *rsp++);
  30. Print(L"%016x\r\n", *rsp++);
  31. }
  32. }
  33. #endif
  34. EFI_STATUS EFI_FUNCTION test_failure_callback(void)
  35. {
  36. return EFI_UNSUPPORTED;
  37. }
  38. EFI_STATUS test_failure(void)
  39. {
  40. return uefi_call_wrapper(test_failure_callback, 0);
  41. }
  42. EFI_STATUS EFI_FUNCTION test_call0_callback(void)
  43. {
  44. return EFI_SUCCESS;
  45. }
  46. EFI_STATUS test_call0(void)
  47. {
  48. return uefi_call_wrapper(test_call0_callback, 0);
  49. }
  50. EFI_STATUS EFI_FUNCTION test_call1_callback(UINT32 a)
  51. {
  52. if (a != 0x12345678) {
  53. return EFI_LOAD_ERROR;
  54. }
  55. return EFI_SUCCESS;
  56. }
  57. EFI_STATUS test_call1(void)
  58. {
  59. return uefi_call_wrapper(test_call1_callback, 1,0x12345678);
  60. }
  61. EFI_STATUS EFI_FUNCTION test_call2_callback(UINT32 a, UINT32 b)
  62. {
  63. if (a != 0x12345678) {
  64. return EFI_LOAD_ERROR;
  65. }
  66. if (b != 0x23456789) {
  67. return EFI_INVALID_PARAMETER;
  68. }
  69. return EFI_SUCCESS;
  70. }
  71. EFI_STATUS test_call2(void)
  72. {
  73. return uefi_call_wrapper(test_call2_callback, 2,
  74. 0x12345678, 0x23456789);
  75. }
  76. EFI_STATUS EFI_FUNCTION test_call3_callback(UINT32 a, UINT32 b,
  77. UINT32 c)
  78. {
  79. if (a != 0x12345678)
  80. return EFI_LOAD_ERROR;
  81. if (b != 0x23456789)
  82. return EFI_INVALID_PARAMETER;
  83. if (c != 0x3456789a)
  84. return EFI_UNSUPPORTED;
  85. return EFI_SUCCESS;
  86. }
  87. EFI_STATUS test_call3(void)
  88. {
  89. return uefi_call_wrapper(test_call3_callback, 3,
  90. 0x12345678, 0x23456789, 0x3456789a);
  91. }
  92. EFI_STATUS EFI_FUNCTION test_call4_callback(UINT32 a, UINT32 b,
  93. UINT32 c, UINT32 d)
  94. {
  95. if (a != 0x12345678)
  96. return EFI_LOAD_ERROR;
  97. if (b != 0x23456789)
  98. return EFI_INVALID_PARAMETER;
  99. if (c != 0x3456789a)
  100. return EFI_UNSUPPORTED;
  101. if (d != 0x456789ab)
  102. return EFI_BAD_BUFFER_SIZE;
  103. return EFI_SUCCESS;
  104. }
  105. EFI_STATUS test_call4(void)
  106. {
  107. return uefi_call_wrapper(test_call4_callback, 4,
  108. 0x12345678, 0x23456789, 0x3456789a, 0x456789ab);
  109. }
  110. EFI_STATUS EFI_FUNCTION test_call5_callback(UINT32 a, UINT32 b,
  111. UINT32 c, UINT32 d, UINT32 e)
  112. {
  113. if (a != 0x12345678)
  114. return EFI_LOAD_ERROR;
  115. if (b != 0x23456789)
  116. return EFI_INVALID_PARAMETER;
  117. if (c != 0x3456789a)
  118. return EFI_UNSUPPORTED;
  119. if (d != 0x456789ab)
  120. return EFI_BAD_BUFFER_SIZE;
  121. if (e != 0x56789abc)
  122. return EFI_BUFFER_TOO_SMALL;
  123. return EFI_SUCCESS;
  124. }
  125. EFI_STATUS test_call5(void)
  126. {
  127. return uefi_call_wrapper(test_call5_callback, 5,
  128. 0x12345678, 0x23456789, 0x3456789a, 0x456789ab, 0x56789abc);
  129. }
  130. EFI_STATUS EFI_FUNCTION test_call6_callback(UINT32 a, UINT32 b,
  131. UINT32 c, UINT32 d, UINT32 e, UINT32 f)
  132. {
  133. if (a != 0x12345678)
  134. return EFI_LOAD_ERROR;
  135. if (b != 0x23456789)
  136. return EFI_INVALID_PARAMETER;
  137. if (c != 0x3456789a)
  138. return EFI_UNSUPPORTED;
  139. if (d != 0x456789ab)
  140. return EFI_BAD_BUFFER_SIZE;
  141. if (e != 0x56789abc)
  142. return EFI_BUFFER_TOO_SMALL;
  143. if (f != 0x6789abcd)
  144. return EFI_NOT_READY;
  145. return EFI_SUCCESS;
  146. }
  147. EFI_STATUS test_call6(void)
  148. {
  149. return uefi_call_wrapper(test_call6_callback, 6,
  150. 0x12345678, 0x23456789, 0x3456789a, 0x456789ab, 0x56789abc,
  151. 0x6789abcd);
  152. }
  153. EFI_STATUS EFI_FUNCTION test_call7_callback(UINT32 a, UINT32 b,
  154. UINT32 c, UINT32 d, UINT32 e, UINT32 f, UINT32 g)
  155. {
  156. if (a != 0x12345678)
  157. return EFI_LOAD_ERROR;
  158. if (b != 0x23456789)
  159. return EFI_INVALID_PARAMETER;
  160. if (c != 0x3456789a)
  161. return EFI_UNSUPPORTED;
  162. if (d != 0x456789ab)
  163. return EFI_BAD_BUFFER_SIZE;
  164. if (e != 0x56789abc)
  165. return EFI_BUFFER_TOO_SMALL;
  166. if (f != 0x6789abcd)
  167. return EFI_NOT_READY;
  168. if (g != 0x789abcde)
  169. return EFI_DEVICE_ERROR;
  170. return EFI_SUCCESS;
  171. }
  172. EFI_STATUS test_call7(void)
  173. {
  174. return uefi_call_wrapper(test_call7_callback, 7,
  175. 0x12345678, 0x23456789, 0x3456789a, 0x456789ab,
  176. 0x56789abc, 0x6789abcd, 0x789abcde);
  177. }
  178. EFI_STATUS EFI_FUNCTION test_call8_callback(UINT32 a, UINT32 b,
  179. UINT32 c, UINT32 d, UINT32 e, UINT32 f, UINT32 g, UINT32 h)
  180. {
  181. if (a != 0x12345678)
  182. return EFI_LOAD_ERROR;
  183. if (b != 0x23456789)
  184. return EFI_INVALID_PARAMETER;
  185. if (c != 0x3456789a)
  186. return EFI_UNSUPPORTED;
  187. if (d != 0x456789ab)
  188. return EFI_BAD_BUFFER_SIZE;
  189. if (e != 0x56789abc)
  190. return EFI_BUFFER_TOO_SMALL;
  191. if (f != 0x6789abcd)
  192. return EFI_NOT_READY;
  193. if (g != 0x789abcde)
  194. return EFI_DEVICE_ERROR;
  195. if (h != 0x89abcdef)
  196. return EFI_WRITE_PROTECTED;
  197. return EFI_SUCCESS;
  198. }
  199. EFI_STATUS test_call8(void)
  200. {
  201. return uefi_call_wrapper(test_call8_callback, 8,
  202. 0x12345678,
  203. 0x23456789,
  204. 0x3456789a,
  205. 0x456789ab,
  206. 0x56789abc,
  207. 0x6789abcd,
  208. 0x789abcde,
  209. 0x89abcdef);
  210. }
  211. EFI_STATUS EFI_FUNCTION test_call9_callback(UINT32 a, UINT32 b,
  212. UINT32 c, UINT32 d, UINT32 e, UINT32 f, UINT32 g, UINT32 h, UINT32 i)
  213. {
  214. if (a != 0x12345678)
  215. return EFI_LOAD_ERROR;
  216. if (b != 0x23456789)
  217. return EFI_INVALID_PARAMETER;
  218. if (c != 0x3456789a)
  219. return EFI_UNSUPPORTED;
  220. if (d != 0x456789ab)
  221. return EFI_BAD_BUFFER_SIZE;
  222. if (e != 0x56789abc)
  223. return EFI_BUFFER_TOO_SMALL;
  224. if (f != 0x6789abcd)
  225. return EFI_NOT_READY;
  226. if (g != 0x789abcde)
  227. return EFI_DEVICE_ERROR;
  228. if (h != 0x89abcdef)
  229. return EFI_WRITE_PROTECTED;
  230. if (i != 0x9abcdef0)
  231. return EFI_OUT_OF_RESOURCES;
  232. return EFI_SUCCESS;
  233. }
  234. EFI_STATUS test_call9(void)
  235. {
  236. return uefi_call_wrapper(test_call9_callback, 9,
  237. 0x12345678,
  238. 0x23456789,
  239. 0x3456789a,
  240. 0x456789ab,
  241. 0x56789abc,
  242. 0x6789abcd,
  243. 0x789abcde,
  244. 0x89abcdef,
  245. 0x9abcdef0);
  246. }
  247. extern EFI_STATUS test_call10(void);
  248. EFI_STATUS EFI_FUNCTION test_call10_callback(UINT32 a, UINT32 b,
  249. UINT32 c, UINT32 d, UINT32 e, UINT32 f, UINT32 g, UINT32 h, UINT32 i,
  250. UINT32 j)
  251. {
  252. if (a != 0x12345678)
  253. return EFI_LOAD_ERROR;
  254. if (b != 0x23456789)
  255. return EFI_INVALID_PARAMETER;
  256. if (c != 0x3456789a)
  257. return EFI_UNSUPPORTED;
  258. if (d != 0x456789ab)
  259. return EFI_BAD_BUFFER_SIZE;
  260. if (e != 0x56789abc)
  261. return EFI_BUFFER_TOO_SMALL;
  262. if (f != 0x6789abcd)
  263. return EFI_NOT_READY;
  264. if (g != 0x789abcde)
  265. return EFI_DEVICE_ERROR;
  266. if (h != 0x89abcdef)
  267. return EFI_WRITE_PROTECTED;
  268. if (i != 0x9abcdef0)
  269. return EFI_OUT_OF_RESOURCES;
  270. if (j != 0xabcdef01)
  271. return EFI_VOLUME_CORRUPTED;
  272. return EFI_SUCCESS;
  273. }
  274. EFI_STATUS test_call10(void)
  275. {
  276. return uefi_call_wrapper(test_call10_callback, 10,
  277. 0x12345678,
  278. 0x23456789,
  279. 0x3456789a,
  280. 0x456789ab,
  281. 0x56789abc,
  282. 0x6789abcd,
  283. 0x789abcde,
  284. 0x89abcdef,
  285. 0x9abcdef0,
  286. 0xabcdef01);
  287. }
  288. EFI_STATUS
  289. efi_main (EFI_HANDLE *image, EFI_SYSTEM_TABLE *systab)
  290. {
  291. EFI_STATUS rc = EFI_SUCCESS;
  292. InitializeLib(image, systab);
  293. PoolAllocationType = 2; /* klooj */
  294. #ifndef __x86_64__
  295. uefi_call_wrapper(systab->ConOut->OutputString, 2, systab->ConOut,
  296. L"This test is only valid on x86_64\n");
  297. return EFI_UNSUPPORTED;
  298. #endif
  299. __asm__ volatile("out %0,%1" : : "a" ((uint8_t)0x14), "dN" (0x80));
  300. Print(L"Hello\r\n");
  301. rc = test_failure();
  302. if (EFI_ERROR(rc)) {
  303. Print(L"Returning Failure works\n");
  304. } else {
  305. Print(L"Returning failure doesn't work.\r\n");
  306. Print(L"%%rax was 0x%016x, should have been 0x%016x\n",
  307. rc, EFI_UNSUPPORTED);
  308. return EFI_INVALID_PARAMETER;
  309. }
  310. rc = test_call0();
  311. if (!EFI_ERROR(rc)) {
  312. Print(L"0 args works just fine here.\r\n");
  313. } else {
  314. Print(L"0 args failed: 0x%016x\n", rc);
  315. return rc;
  316. }
  317. rc = test_call1();
  318. if (!EFI_ERROR(rc)) {
  319. Print(L"1 arg works just fine here.\r\n");
  320. } else {
  321. Print(L"1 arg failed: 0x%016x\n", rc);
  322. return rc;
  323. }
  324. rc = test_call2();
  325. if (!EFI_ERROR(rc)) {
  326. Print(L"2 args works just fine here.\r\n");
  327. } else {
  328. Print(L"2 args failed: 0x%016x\n", rc);
  329. return rc;
  330. }
  331. rc = test_call3();
  332. if (!EFI_ERROR(rc)) {
  333. Print(L"3 args works just fine here.\r\n");
  334. } else {
  335. Print(L"3 args failed: 0x%016x\n", rc);
  336. return rc;
  337. }
  338. rc = test_call4();
  339. if (!EFI_ERROR(rc)) {
  340. Print(L"4 args works just fine here.\r\n");
  341. } else {
  342. Print(L"4 args failed: 0x%016x\n", rc);
  343. return rc;
  344. }
  345. rc = test_call5();
  346. if (!EFI_ERROR(rc)) {
  347. Print(L"5 args works just fine here.\r\n");
  348. } else {
  349. Print(L"5 args failed: 0x%016x\n", rc);
  350. return rc;
  351. }
  352. rc = test_call6();
  353. if (!EFI_ERROR(rc)) {
  354. Print(L"6 args works just fine here.\r\n");
  355. } else {
  356. Print(L"6 args failed: 0x%016x\n", rc);
  357. return rc;
  358. }
  359. rc = test_call7();
  360. if (!EFI_ERROR(rc)) {
  361. Print(L"7 args works just fine here.\r\n");
  362. } else {
  363. Print(L"7 args failed: 0x%016x\n", rc);
  364. return rc;
  365. }
  366. rc = test_call8();
  367. if (!EFI_ERROR(rc)) {
  368. Print(L"8 args works just fine here.\r\n");
  369. } else {
  370. Print(L"8 args failed: 0x%016x\n", rc);
  371. return rc;
  372. }
  373. rc = test_call9();
  374. if (!EFI_ERROR(rc)) {
  375. Print(L"9 args works just fine here.\r\n");
  376. } else {
  377. Print(L"9 args failed: 0x%016x\n", rc);
  378. return rc;
  379. }
  380. rc = test_call10();
  381. if (!EFI_ERROR(rc)) {
  382. Print(L"10 args works just fine here.\r\n");
  383. } else {
  384. Print(L"10 args failed: 0x%016x\n", rc);
  385. return rc;
  386. }
  387. return rc;
  388. }