Newer
Older
honey-os / src / kernel / service / serviceSyscalls.c
  1. #include "elf.h"
  2. #include <service.h>
  3. #include <stdint.h>
  4. #include <stringmap.h>
  5.  
  6. extern Syscall *currentSyscall;
  7.  
  8. uint32_t getServiceId(Service *searchService) {
  9. uint32_t i = 0;
  10. foreach (services, Service *, service, {
  11. if (service == searchService) {
  12. return i;
  13. }
  14. i++;
  15. })
  16. ;
  17. return i;
  18. }
  19.  
  20. void handleGetServiceIdSyscall(Syscall *call) {
  21. call->returnValue = ((Service *)call->service)->id;
  22. }
  23.  
  24. void handleGetServiceSyscall(Syscall *call) {
  25. uint32_t i = 0;
  26. Service *callService = call->service;
  27. char *name = retrieveString(call->parameters[0]);
  28. if (!name) {
  29. return;
  30. }
  31. foreach (services, Service *, service, {
  32. if (stringEquals(service->name, name)) {
  33. call->returnValue = i;
  34. return;
  35. }
  36. i++;
  37. })
  38. ;
  39. }
  40.  
  41. void handleGetFunctionSyscall(Syscall *call) {
  42. uint32_t i = 0;
  43. Service *callService = call->service;
  44. char *name = retrieveString(call->parameters[1]);
  45. if (!name) {
  46. return;
  47. }
  48. Service *providerService = listGet(services, call->parameters[0]);
  49. foreach (providerService->functions, ServiceFunction *, provider, {
  50. if (stringEquals(provider->name, name)) {
  51. call->returnValue = i;
  52. return;
  53. }
  54. i++;
  55. })
  56. ;
  57. }
  58.  
  59. void handleCreateFunctionSyscall(Syscall *call) {
  60. char *name = retrieveString(call->parameters[0]);
  61. if (!name) {
  62. return;
  63. }
  64. ServiceFunction *function = malloc(sizeof(ServiceFunction));
  65. Service *service = call->service;
  66. function->name = name;
  67. function->address = PTR(call->parameters[1]);
  68. function->service = call->service;
  69. call->returnValue = listCount(service->functions);
  70. listAdd(&service->functions, function);
  71. }
  72.  
  73. void handleRequestSyscall(Syscall *call) {
  74. Service *service = call->service;
  75. Service *providerService = listGet(services, call->parameters[0]);
  76. if (!providerService)
  77. return;
  78. ServiceFunction *function =
  79. listGet(providerService->functions, call->parameters[1]);
  80. if (!function)
  81. return;
  82. scheduleFunction(function, call, call->parameters[2], call->parameters[3],
  83. service->nameHash, service->id);
  84. call->avoidReschedule = true;
  85. }
  86.  
  87. void handleLookupSymbolSyscall(Syscall *call) {
  88. Service *service = listGet(services, call->parameters[0]);
  89. uint32_t location = call->parameters[1];
  90. for (uint32_t i = 0; i < service->symbolTableSize / sizeof(SymbolEntry);
  91. i++) {
  92. SymbolEntry *entry = &service->symbolTable[i];
  93. if (location >= entry->value &&
  94. location <= entry->value + entry->size) {
  95. char *name = service->stringTable + entry->name;
  96. call->returnValue = insertString(name);
  97. return;
  98. }
  99. }
  100. }
  101.  
  102. void handleStackContainsSyscall(Syscall *call) {
  103. Syscall *currentCall = call;
  104. while (currentCall) {
  105. if (((Service *)currentCall->service)->id == call->parameters[0]) {
  106. call->returnValue = 1;
  107. return;
  108. }
  109. currentCall = currentCall->respondingTo;
  110. }
  111. call->returnValue = 0;
  112. }
  113.  
  114. void handleForkSyscall(Syscall *call) {
  115. Service *service = call->service;
  116. ServiceFunction function = {
  117. .service = service,
  118. .address = PTR(call->parameters[0]),
  119. .name = "forked function",
  120. };
  121. scheduleFunction(&function, NULL, call->parameters[1], call->parameters[2], call->parameters[3]);
  122. }