1
0

helpers.asm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ;
  2. ; helpers.asm
  3. ;
  4. ; Copyright (C) 2016 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  5. ;
  6. ; This program is free software: you can redistribute it and/or modify
  7. ; it under the terms of the GNU Affero General Public License as
  8. ; published by the Free Software Foundation, either version 3 of the
  9. ; License, or (at your option) any later version.
  10. ;
  11. ; This program is distributed in the hope that it will be useful,
  12. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ; GNU Affero General Public License for more details.
  15. ;
  16. ; You should have received a copy of the GNU Affero General Public License
  17. ; along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. ;
  19. CONTEXT_SWITCH_MAGIC EQU 0xDEADBEEF
  20. bits 32
  21. global init_cpu_gdt
  22. global vm86_start
  23. global save_kernel_handler
  24. global syscall_function
  25. global reschedule
  26. extern set_exception_handler
  27. extern set_kernel_esp
  28. extern scheduler
  29. gdtr DW 0
  30. DD 0
  31. ;********************************************************************************
  32. ; void init_cpu_gdt(gdt_descriptor_t *table,
  33. ; size_t table_size,
  34. ; word_t code_selector,
  35. ; word_t data_selector,
  36. ; word_t tss_selector)
  37. ;********************************************************************************
  38. init_cpu_gdt: push ebp
  39. mov ebp, esp
  40. pushfd
  41. cli
  42. mov eax, dword [ebp + 8]
  43. mov dword [gdtr + 2], eax
  44. mov eax, dword [ebp + 12]
  45. dec eax
  46. mov word [gdtr], ax
  47. mov eax, dword [ebp + 16]
  48. mov word [.code_seg], ax
  49. mov eax, dword [ebp + 20]
  50. lgdt [gdtr]
  51. DB 0xEA
  52. DD .continue
  53. .code_seg: DW 0
  54. .continue: mov es, ax
  55. mov ss, ax
  56. mov ds, ax
  57. mov fs, ax
  58. mov gs, ax
  59. mov eax, dword [ebp + 24]
  60. ltr ax
  61. popfd
  62. leave
  63. ret
  64. ;********************************************************************************
  65. ; void vm86_start(vm86_registers_t regs)
  66. ;********************************************************************************
  67. vm86_start: pushfd
  68. push cs
  69. push dword [esp + 8]
  70. push 0
  71. pushad
  72. push ds
  73. push esp
  74. call set_kernel_esp
  75. cli
  76. add esp, 60
  77. popad
  78. iret
  79. ;********************************************************************************
  80. ; void save_kernel_handler(exception_handler_t *old_handler)
  81. ;********************************************************************************
  82. save_kernel_handler: pushfd
  83. push cs
  84. push dword [esp + 8]
  85. push 0
  86. pushad
  87. add dword [esp + 12], 16
  88. push ds
  89. push dword [esp + 56]
  90. push 0
  91. lea eax, [esp + 8]
  92. push eax
  93. call set_exception_handler
  94. add esp, 64
  95. xor eax, eax
  96. ret
  97. ;********************************************************************************
  98. ; qword_t syscall_function(void *function,
  99. ; dword_t *parameters,
  100. ; size_t parameters_size)
  101. ;********************************************************************************
  102. syscall_function: push ebp
  103. mov ebp, esp
  104. mov eax, dword [ebp + 0x08]
  105. mov esi, dword [ebp + 0x0C]
  106. mov ecx, dword [ebp + 0x10]
  107. or ecx, ecx
  108. jz .no_params
  109. sub esp, ecx
  110. mov edi, esp
  111. cld
  112. rep movsb
  113. .no_params: call eax
  114. add esp, dword [ebp + 0x10]
  115. pop ebp
  116. ret
  117. ;********************************************************************************
  118. ; void reschedule(void)
  119. ;********************************************************************************
  120. reschedule: push cs
  121. push dword [esp + 4]
  122. push 0
  123. pushad
  124. pushfd
  125. cli
  126. pop eax
  127. mov dword [esp + 44], eax
  128. add dword [esp + 12], 20
  129. push ds
  130. push esp
  131. call scheduler
  132. add esp, 4
  133. cmp dword [esp + 0x24], CONTEXT_SWITCH_MAGIC
  134. jnz .no_stack_switch
  135. mov esp, dword [esp + 0x10]
  136. .no_stack_switch: pop eax
  137. mov ds, ax
  138. mov es, ax
  139. mov fs, ax
  140. mov gs, ax
  141. popad
  142. add esp, 4
  143. iret