1
0

kernel.asm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. ;; This file is part of asmc, a bootstrapping OS with minimal seed
  2. ;; Copyright (C) 2018-2019 Giovanni Mascellani <gio@debian.org>
  3. ;; https://gitlab.com/giomasce/asmc
  4. ;; This program is free software: you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. STACK_SIZE equ 65536
  15. ;; STACK_SIZE equ 8388608
  16. MAX_OPEN_FILE_NUM equ 1024
  17. FILE_RECORD_SIZE equ 16
  18. FILE_RECORD_SIZE_LOG equ 4
  19. section .bss
  20. heap_ptr:
  21. resd 1
  22. section .data
  23. %ifdef DEBUG
  24. str_exit:
  25. db 'The execution has finished, bye bye...'
  26. db NEWLINE
  27. db 0
  28. str_panic:
  29. db 'PANIC!'
  30. str_newline:
  31. db NEWLINE
  32. db 0
  33. str_hello_asmc:
  34. db 'Hello, asmc!'
  35. db NEWLINE
  36. db 0
  37. str_init_asm_symbols_table:
  38. db 'Initializing symbols table... '
  39. db 0
  40. str_done:
  41. db 'done!'
  42. db NEWLINE
  43. db 0
  44. %endif
  45. str_platform_panic:
  46. db 'platform_panic'
  47. str_empty:
  48. db 0
  49. str_platform_allocate:
  50. db 'platform_allocate'
  51. db 0
  52. str_platform_get_symbol:
  53. db 'platform_get_symbol'
  54. db 0
  55. str_platform_walk_initrd:
  56. db 'platform_walk_initrd'
  57. db 0
  58. section .text
  59. entry:
  60. ;; Make it double sure that we do not have interrupts around
  61. cli
  62. ;; Use the multiboot header as temporary stack
  63. mov esp, temp_stack_top
  64. ;; Initialize the BSS
  65. ;; mov ecx, begin_bss
  66. ;; xor eax, eax
  67. ;; entry_bss_loop:
  68. ;; mov [ecx], al
  69. ;; inc ecx
  70. ;; cmp ecx, end_bss
  71. ;; jne entry_bss_loop
  72. ;; Find the end of the ar initrd
  73. mov ecx, str_empty
  74. call walk_initrd
  75. ;; Initialize the stack and the heap, aligning to 16 bytes
  76. sub edx, 1
  77. or edx, 0xf
  78. add edx, 1
  79. add edx, STACK_SIZE
  80. mov esp, edx
  81. mov [heap_ptr], edx
  82. %ifdef DEBUG
  83. ;; Initialize stdout
  84. call stdout_setup
  85. ;; call enable_single_stepping
  86. %endif
  87. %ifdef DEBUG
  88. ;; Log
  89. mov esi, str_hello_asmc
  90. call log
  91. ;; Log
  92. mov esi, str_init_asm_symbols_table
  93. call log
  94. %endif
  95. ;; Init symbol table
  96. call init_symbols
  97. ;; Expose some kernel symbols
  98. call init_kernel_api
  99. %ifdef DEBUG
  100. ;; Log
  101. mov esi, str_done
  102. call log
  103. %endif
  104. ;; Call start
  105. call start
  106. %ifdef DEBUG
  107. mov esi, str_exit
  108. call log
  109. %endif
  110. mov eax, 0
  111. jmp shutdown
  112. platform_panic:
  113. panic:
  114. jmp shutdown
  115. %ifdef DEBUG
  116. ;; Write an exit string
  117. mov esi, str_panic
  118. call log
  119. %endif
  120. mov eax, 1
  121. jmp shutdown
  122. %ifdef DEBUG
  123. ;; Input character in CL
  124. ;; Destroys: EAX, EDX
  125. write:
  126. jmp write_console
  127. ;; Input string in ESI
  128. ;; Destroys: EAX, ECX, EDX, ESI
  129. log:
  130. mov cl, [esi]
  131. test cl, cl
  132. jz ret_simple
  133. call write
  134. inc esi
  135. jmp log
  136. %endif
  137. platform_allocate:
  138. mov eax, [esp+4]
  139. ;; Size in EAX
  140. ;; Destroys: ECX
  141. ;; Returns: EAX
  142. allocate:
  143. dec eax
  144. or eax, 0x3
  145. inc eax
  146. mov ecx, eax
  147. mov eax, [heap_ptr]
  148. add ecx, eax
  149. mov [heap_ptr], ecx
  150. ret
  151. platform_walk_initrd:
  152. ;; Decode argument
  153. mov ecx, [esp+4]
  154. ;; Call walk_initrd protecting registers
  155. push esi
  156. push edi
  157. call walk_initrd
  158. pop edi
  159. pop esi
  160. ;; Save result in the pointers indicated in the stack
  161. mov ecx, [esp+8]
  162. mov [ecx], edx
  163. mov ecx, [esp+12]
  164. mov [ecx], eax
  165. ret
  166. ;; Initialize the symbols table with the "kernel API"
  167. init_kernel_api:
  168. mov edx, 0
  169. mov ecx, platform_panic
  170. mov eax, str_platform_panic
  171. call add_symbol
  172. mov edx, 1
  173. mov ecx, platform_allocate
  174. mov eax, str_platform_allocate
  175. call add_symbol
  176. mov edx, 2
  177. mov ecx, platform_get_symbol
  178. mov eax, str_platform_get_symbol
  179. call add_symbol
  180. mov edx, 3
  181. mov ecx, platform_walk_initrd
  182. mov eax, str_platform_walk_initrd
  183. call add_symbol
  184. ret
  185. ;; int platform_get_symbol(char *name, int *arity)
  186. ;; similar as find_symbol, but panic if it does not exist; retuns
  187. ;; the location and put the arity in *arity if arity is not null
  188. platform_get_symbol:
  189. ;; Call find_symbol
  190. mov edx, [esp+4]
  191. call find_symbol
  192. ;; Panic if it does not exist
  193. cmp eax, 0
  194. je platform_panic
  195. ;; Save arity if required
  196. mov eax, [esp+8]
  197. cmp eax, 0
  198. je platform_get_symbol_ret
  199. mov [eax], edx
  200. platform_get_symbol_ret:
  201. ;; Return the symbol location
  202. mov eax, ecx
  203. ret