Advertisement
Guest User

main.c

a guest
Apr 24th, 2023
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. #include <allocman/allocman.h>
  2. #include <allocman/bootstrap.h>
  3. #include <allocman/vka.h>
  4. #include <cpio/cpio.h>
  5. #include <sel4platsupport/bootinfo.h>
  6. #include <sel4utils/process.h>
  7. #include <sel4utils/vspace.h>
  8. #include <simple-default/simple-default.h>
  9. #include <simple/simple.h>
  10. #include <stdio.h>
  11.  
  12. #define ALLOCATOR_STATIC_POOL_SIZE (BIT(seL4_PageBits) * 10)
  13. #define ALLOCATOR_VIRTUAL_POOL_SIZE (BIT(seL4_PageBits) * 100)
  14.  
  15.  
  16. /* global state */
  17. allocman_t *allocman;
  18. vka_object_t endpoint;
  19. simple_t simple;
  20. vka_t vka;
  21. vspace_t vspace;
  22. cspacepath_t IPCCapRecv;
  23.  
  24. UNUSED static char allocman_mem[ALLOCATOR_STATIC_POOL_SIZE];
  25. UNUSED static sel4utils_alloc_data_t data;
  26.  
  27. #define APP_BADGE 1
  28. sel4utils_process_t app_pcb;
  29. vka_object_t app_ep;
  30.  
  31. void init(void)
  32. {
  33. UNUSED int err = 0;
  34.  
  35. NAME_THREAD(seL4_CapInitThreadTCB, "root");
  36.  
  37. // get boot info
  38. seL4_BootInfo *info = platsupport_get_bootinfo();
  39. ZF_LOGF_IF(info == NULL, "failed to get bootinfo\n");
  40.  
  41. // init simple
  42. simple_default_init_bootinfo(&simple, info);
  43.  
  44. // initialise the allocator
  45. allocman = bootstrap_use_bootinfo(info, ALLOCATOR_STATIC_POOL_SIZE, allocman_mem);
  46. ZF_LOGF_IF(allocman == NULL, "failed to initialize allocator\n");
  47.  
  48. // create a vka (interface for interacting with the kernel allocator)
  49. allocman_make_vka(&vka, allocman);
  50.  
  51. // manage our own root server VSpace using this newly created allocator.
  52. err =
  53. sel4utils_bootstrap_vspace_with_bootinfo_leaky(&vspace, &data, seL4_CapInitThreadPD, &vka, info);
  54. ZF_LOGF_IFERR(err, "failed to bootstrap root vspace\n");
  55.  
  56. /* fill the allocator with virtual memory */
  57. void *vaddr;
  58. UNUSED reservation_t virtual_reservation;
  59.  
  60. virtual_reservation = vspace_reserve_range(&vspace, ALLOCATOR_VIRTUAL_POOL_SIZE, seL4_AllRights, 1, &vaddr);
  61. ZF_LOGF_IF(virtual_reservation.res == NULL, "failed to provide virtual memory");
  62. bootstrap_configure_virtual_pool(allocman, vaddr, ALLOCATOR_VIRTUAL_POOL_SIZE, seL4_CapInitThreadPD);
  63.  
  64. err = vka_alloc_endpoint(&vka, &endpoint);
  65. ZF_LOGF_IFERR(err, "failed to allocate endpoint\n");
  66.  
  67. printf("root initialised\n");
  68. }
  69.  
  70. void spawn(simple_t *simple, sel4utils_process_t *pcb, seL4_Word badge, char *name)
  71. {
  72. ZF_LOGF_IF(name == NULL, "invalid name, refuse to create new process");
  73.  
  74. // configure process
  75. sel4utils_process_config_t config = process_config_default_simple(simple, name, seL4_MaxPrio);
  76. int error = sel4utils_configure_process_custom(pcb, &vka, &vspace, config);
  77. ZF_LOGF_IFERR(error, "failed to setup new process\n");
  78.  
  79. // mint ep cap
  80. cspacepath_t re_path;
  81. vka_cspace_make_path(&vka, endpoint.cptr, &re_path);
  82. seL4_CPtr ep = sel4utils_mint_cap_to_process(pcb, re_path, seL4_AllRights, badge);
  83.  
  84. // prepare args, and spawn the process
  85. char string_args[1][WORD_STRING_SIZE];
  86. char *argv[1];
  87. sel4utils_create_word_args(string_args, argv, 1, ep);
  88.  
  89. printf("starting %s\n", name);
  90. error = sel4utils_spawn_process_v(pcb, &vka, &vspace, 1, argv, 1);
  91. ZF_LOGF_IFERR(error, "failed to spawn process %s\n", name);
  92.  
  93. return 0;
  94. }
  95.  
  96. sel4utils_process_t *pcb_from_badge(seL4_Word badge)
  97. {
  98. return badge == APP_BADGE ? &app_pcb : NULL /* ... */;
  99. }
  100.  
  101. void handle_req_ep(sel4utils_process_t *pcb)
  102. {
  103. printf("app requested new endpoint from root\n");
  104.  
  105. // allocate new endpoint, and track it
  106. vka_alloc_endpoint(&vka, &app_ep);
  107.  
  108. cspacepath_t ep_cap_path;
  109. vka_cspace_make_path(&vka, app_ep.cptr, &ep_cap_path);
  110.  
  111. // seL4_CPtr c = sel4utils_copy_cap_to_process(pcb, &vka, app_ep.cptr);
  112. seL4_CPtr new_ep_cap = sel4utils_mint_cap_to_process(pcb, ep_cap_path,
  113. seL4_AllRights, APP_BADGE);
  114.  
  115. seL4_MessageInfo_t info = seL4_MessageInfo_new(0, 0, 1, 1);
  116. seL4_SetCap(0, new_ep_cap);
  117. seL4_SetMR(0, 0x2E2E2E2E);
  118. seL4_Reply(info);
  119.  
  120. printf("root send to ep\n");
  121. seL4_MessageInfo_t msg = seL4_MessageInfo_new(0, 0, 0, 1);
  122. seL4_SetMR(0, 0xEFEFEFEF);
  123. seL4_Send(app_ep.cptr, msg);
  124. }
  125.  
  126. int main(void)
  127. {
  128. printf("==========================================================\n");
  129. printf("== main started ==\n");
  130. printf("==========================================================\n");
  131.  
  132. // initialise root task
  133. init();
  134.  
  135. printf("root ep: 0x%x\n", endpoint.cptr);
  136.  
  137. // spawn processes
  138. spawn(&simple, &app_pcb, APP_BADGE, "app");
  139.  
  140. // wait for IPC request
  141. seL4_Word badge;
  142. seL4_MessageInfo_t msg = seL4_Recv(endpoint.cptr, &badge);
  143.  
  144. handle_req_ep(pcb_from_badge(badge));
  145.  
  146. while (true) ;
  147.  
  148. return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement