fasm.g 6.2 KB

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