fasm.g 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. # This file is part of asmc, a bootstrapping OS with minimal seed
  2. # Copyright (C) 2018 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. fun fasm_open 1 {
  15. $filename
  16. @filename 0 param = ;
  17. "FASM: opening file " 1 platform_log ;
  18. filename 1 platform_log ;
  19. "\n" 1 platform_log ;
  20. $fd
  21. @fd filename vfs_open = ;
  22. fd ret ;
  23. }
  24. fun fasm_create 1 {
  25. $filename
  26. @filename 0 param = ;
  27. "FASM: creating file " 1 platform_log ;
  28. filename 1 platform_log ;
  29. "\n" 1 platform_log ;
  30. $fd
  31. @fd filename vfs_open = ;
  32. if fd 0 == {
  33. 0 ret ;
  34. }
  35. fd vfs_truncate ;
  36. fd ret ;
  37. }
  38. fun fasm_read 3 {
  39. $fd
  40. $buf
  41. $count
  42. @fd 2 param = ;
  43. @buf 1 param = ;
  44. @count 0 param = ;
  45. $i
  46. @i 0 = ;
  47. #"Reading " 1 platform_log ;
  48. #count itoa 1 platform_log ;
  49. #" bytes: " 1 platform_log ;
  50. while i count < {
  51. $tmp
  52. @tmp fd vfs_read = ;
  53. #tmp 1 platform_write_char ;
  54. if tmp 0xffffffff == {
  55. 0 ret ;
  56. }
  57. buf i + tmp =c ;
  58. @i i 1 + = ;
  59. }
  60. #" done!\n" 1 platform_log ;
  61. 1 ret ;
  62. }
  63. fun fasm_write 3 {
  64. $fd
  65. $buf
  66. $count
  67. @fd 2 param = ;
  68. @buf 1 param = ;
  69. @count 0 param = ;
  70. $i
  71. @i 0 = ;
  72. "Writing " 1 platform_log ;
  73. count itoa 1 platform_log ;
  74. " bytes: " 1 platform_log ;
  75. while i count < {
  76. $tmp
  77. @tmp buf i + **c =c ;
  78. fd tmp vfs_write ;
  79. @i i 1 + = ;
  80. }
  81. 1 ret ;
  82. }
  83. fun fasm_lseek 3 {
  84. $fd
  85. $off
  86. $whence
  87. @fd 2 param = ;
  88. @off 1 param = ;
  89. @whence 0 param = ;
  90. $res
  91. @res fd off whence vfs_seek = ;
  92. #"Seek to " 1 platform_log ;
  93. #res itoa 1 platform_log ;
  94. #"\n" 1 platform_log ;
  95. res ret ;
  96. }
  97. fun fasm_close 1 {
  98. $fd
  99. @fd 0 param = ;
  100. "FASM: close\n" 1 platform_log ;
  101. fd vfs_close ;
  102. }
  103. fun fasm_fatal_error 1 {
  104. $msg
  105. @msg 0 param = ;
  106. "FASM FATAL ERROR: " 1 platform_log ;
  107. msg 1 platform_log ;
  108. "\n" 1 platform_log ;
  109. }
  110. fun fasm_assembler_error 1 {
  111. $msg
  112. @msg 0 param = ;
  113. "FASM ASSEMBLER ERROR: " 1 platform_log ;
  114. msg 1 platform_log ;
  115. "\n" 1 platform_log ;
  116. }
  117. fun fasm_display_block 2 {
  118. $msg
  119. $len
  120. @msg 1 param = ;
  121. @len 0 param = ;
  122. $i
  123. @i 0 = ;
  124. while i len < {
  125. msg i + **c 1 platform_write_char ;
  126. @i i 1 + = ;
  127. }
  128. }
  129. $instr_num
  130. $ret_instr_enter
  131. fun error_additional_handler 0 {
  132. "Fault happened after retiring " 1 platform_log ;
  133. read_ret_instr ret_instr_enter - itoa 1 platform_log ;
  134. " instructions (according to PMC).\n" 1 platform_log ;
  135. "Fault happened after executing " 1 platform_log ;
  136. instr_num itoa 1 platform_log ;
  137. " instructions (according to single step counter).\n" 1 platform_log ;
  138. }
  139. $dumping
  140. $breakpoint
  141. $breakpoint_instr_num
  142. fun single_step_handler 1 {
  143. $regs
  144. @regs 0 param = ;
  145. $ip
  146. @ip regs 0x20 + ** = ;
  147. # "Instruction number " 1 platform_log ;
  148. # instr_num itoa 1 platform_log ;
  149. # "\n" 1 platform_log ;
  150. @instr_num instr_num 1 + = ;
  151. if breakpoint_instr_num 0 != instr_num breakpoint_instr_num == && {
  152. @dumping 1 = ;
  153. }
  154. if ip breakpoint == {
  155. @dumping 1 = ;
  156. }
  157. if dumping {
  158. "EAX=" 1 platform_log ;
  159. regs 0x1c + ** itoa 1 platform_log ;
  160. ", EBX=" 1 platform_log ;
  161. regs 0x10 + ** itoa 1 platform_log ;
  162. ", ECX=" 1 platform_log ;
  163. regs 0x18 + ** itoa 1 platform_log ;
  164. ", EDX=" 1 platform_log ;
  165. regs 0x14 + ** itoa 1 platform_log ;
  166. ", ESI=" 1 platform_log ;
  167. regs 0x4 + ** itoa 1 platform_log ;
  168. ", EDI=" 1 platform_log ;
  169. regs 0x0 + ** itoa 1 platform_log ;
  170. ", ESP=" 1 platform_log ;
  171. regs 0xc + ** itoa 1 platform_log ;
  172. ", EBP=" 1 platform_log ;
  173. regs 0x8 + ** itoa 1 platform_log ;
  174. "\n" 1 platform_log ;
  175. "Instruction number " 1 platform_log ;
  176. instr_num itoa 1 platform_log ;
  177. ": EIP=" 1 platform_log ;
  178. ip itoa 1 platform_log ;
  179. ", code: " 1 platform_log ;
  180. ip 32 dump_mem ;
  181. "\n\n" 1 platform_log ;
  182. }
  183. }
  184. fun compile_fasm 0 {
  185. $filename
  186. @filename "/disk1/fasm/fasm.asm" = ;
  187. 0x10000 @error_additional_handler = ;
  188. @ret_instr_enter read_ret_instr = ;
  189. "Retired instruction counter before compiling fasm: " 1 platform_log ;
  190. read_ret_instr itoa 1 platform_log ;
  191. "\n" 1 platform_log ;
  192. # Compile fasm
  193. $ctx
  194. @ctx asmctx_init = ;
  195. ctx ASMCTX_DEBUG take_addr 0 = ;
  196. $fd
  197. @fd filename vfs_open = ;
  198. fd "compile_fasm: file does not exist" assert_msg ;
  199. ctx fd asmctx_set_fd ;
  200. ctx asmctx_compile ;
  201. fd vfs_close ;
  202. # Prepare fasm argument list
  203. $input_file
  204. $output_file
  205. @input_file "/disk1/fasm/test.asm" = ;
  206. @output_file "/ram/test.bin" = ;
  207. $main_addr
  208. @main_addr ctx "main" asmctx_get_symbol_addr = ;
  209. $handles
  210. @handles 4 vector_init = ;
  211. handles input_file vector_push_back ;
  212. handles output_file vector_push_back ;
  213. handles @malloc vector_push_back ;
  214. handles @free vector_push_back ;
  215. handles @platform_setjmp vector_push_back ;
  216. handles @platform_longjmp vector_push_back ;
  217. handles @platform_log vector_push_back ;
  218. handles @fasm_open vector_push_back ;
  219. handles @fasm_create vector_push_back ;
  220. handles @fasm_read vector_push_back ;
  221. handles @fasm_write vector_push_back ;
  222. handles @fasm_close vector_push_back ;
  223. handles @fasm_lseek vector_push_back ;
  224. handles @fasm_fatal_error vector_push_back ;
  225. handles @fasm_assembler_error vector_push_back ;
  226. handles @fasm_display_block vector_push_back ;
  227. @ret_instr_enter read_ret_instr = ;
  228. "Retired instruction counter before entering fasm: " 1 platform_log ;
  229. read_ret_instr itoa 1 platform_log ;
  230. "\n" 1 platform_log ;
  231. # Enable single stepping
  232. @instr_num 0 = ;
  233. @dumping 0 = ;
  234. #@breakpoint ctx "fix_tables" asmctx_get_symbol_addr = ;
  235. @breakpoint_instr_num 0 = ;
  236. 0x10010 @single_step_handler = ;
  237. 0x1001c ** \0 ;
  238. # Run fasm
  239. $res
  240. @res handles vector_data main_addr \1 = ;
  241. # Disable single stepping
  242. 0x10014 0 = ;
  243. "Retired instruction counter after exiting fasm: " 1 platform_log ;
  244. read_ret_instr itoa 1 platform_log ;
  245. "\n" 1 platform_log ;
  246. "Executed instruction number after exiting fasm: " 1 platform_log ;
  247. instr_num itoa 1 platform_log ;
  248. "\n" 1 platform_log ;
  249. "fasm returned " 1 platform_log ;
  250. res itoa 1 platform_log ;
  251. "\n" 1 platform_log ;
  252. handles vector_destroy ;
  253. ctx asmctx_destroy ;
  254. }