tcc.c 8.8 KB

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