fasm.g 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. "FASM: reading " 1 platform_log ;
  48. count itoa 1 platform_log ;
  49. " bytes\n" 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. "FASM: writing " 1 platform_log ;
  73. count itoa 1 platform_log ;
  74. " bytes\n" 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. "FASM: seeking 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: closing file\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. "FASM DISPLAY BLOCK: " 1 platform_log ;
  125. while i len < {
  126. msg i + **c 1 platform_write_char ;
  127. @i i 1 + = ;
  128. }
  129. "\n" 1 platform_log ;
  130. }
  131. fun fasm_get_environment_variable 1 {
  132. $var
  133. @var 0 param = ;
  134. "FASM: requesting envvar " 1 platform_log ;
  135. var 1 platform_log ;
  136. "\n" 1 platform_log ;
  137. }
  138. fun fasm_make_timestamp 0 {
  139. "FASM: requesting timestamp\n" 1 platform_log ;
  140. 0 ret ;
  141. }
  142. $instr_num
  143. $ret_instr_enter
  144. fun error_additional_handler 0 {
  145. "Fault happened after retiring " 1 platform_log ;
  146. read_ret_instr ret_instr_enter - itoa 1 platform_log ;
  147. " instructions (according to PMC).\n" 1 platform_log ;
  148. "Fault happened after executing " 1 platform_log ;
  149. instr_num itoa 1 platform_log ;
  150. " instructions (according to single step counter).\n" 1 platform_log ;
  151. }
  152. $dumping
  153. $breakpoint
  154. $breakpoint_instr_num
  155. fun single_step_handler 1 {
  156. $regs
  157. @regs 0 param = ;
  158. $ip
  159. @ip regs 0x20 + ** = ;
  160. # "Instruction number " 1 platform_log ;
  161. # instr_num itoa 1 platform_log ;
  162. # "\n" 1 platform_log ;
  163. @instr_num instr_num 1 + = ;
  164. if breakpoint_instr_num 0 != instr_num breakpoint_instr_num == && {
  165. @dumping 1 = ;
  166. }
  167. if ip breakpoint == {
  168. @dumping 1 = ;
  169. }
  170. if dumping {
  171. "EAX=" 1 platform_log ;
  172. regs 0x1c + ** itoa 1 platform_log ;
  173. ", EBX=" 1 platform_log ;
  174. regs 0x10 + ** itoa 1 platform_log ;
  175. ", ECX=" 1 platform_log ;
  176. regs 0x18 + ** itoa 1 platform_log ;
  177. ", EDX=" 1 platform_log ;
  178. regs 0x14 + ** itoa 1 platform_log ;
  179. ", ESI=" 1 platform_log ;
  180. regs 0x4 + ** itoa 1 platform_log ;
  181. ", EDI=" 1 platform_log ;
  182. regs 0x0 + ** itoa 1 platform_log ;
  183. ", ESP=" 1 platform_log ;
  184. regs 0xc + ** itoa 1 platform_log ;
  185. ", EBP=" 1 platform_log ;
  186. regs 0x8 + ** itoa 1 platform_log ;
  187. "\n" 1 platform_log ;
  188. "Instruction number " 1 platform_log ;
  189. instr_num itoa 1 platform_log ;
  190. ": EIP=" 1 platform_log ;
  191. ip itoa 1 platform_log ;
  192. ", code: " 1 platform_log ;
  193. ip 32 dump_mem ;
  194. "\n\n" 1 platform_log ;
  195. }
  196. }
  197. fun compile_fasm 0 {
  198. $filename
  199. @filename "/disk1/fasm/fasm.asm" = ;
  200. 0x10000 @error_additional_handler = ;
  201. @ret_instr_enter read_ret_instr = ;
  202. "Retired instruction counter before compiling fasm: " 1 platform_log ;
  203. read_ret_instr itoa 1 platform_log ;
  204. "\n" 1 platform_log ;
  205. # Compile fasm
  206. $ctx
  207. @ctx asmctx_init = ;
  208. ctx ASMCTX_DEBUG take_addr 0 = ;
  209. $fd
  210. @fd filename vfs_open = ;
  211. fd "compile_fasm: file does not exist" assert_msg ;
  212. ctx fd asmctx_set_fd ;
  213. ctx asmctx_compile ;
  214. fd vfs_close ;
  215. # Prepare fasm argument list
  216. $input_file
  217. $output_file
  218. @input_file "/disk1/fasm/test.asm" = ;
  219. @output_file "/ram/test.bin" = ;
  220. $main_addr
  221. @main_addr ctx "main" asmctx_get_symbol_addr = ;
  222. $handles
  223. @handles 4 vector_init = ;
  224. handles input_file vector_push_back ;
  225. handles output_file vector_push_back ;
  226. handles @malloc vector_push_back ;
  227. handles @free vector_push_back ;
  228. handles @platform_setjmp vector_push_back ;
  229. handles @platform_longjmp vector_push_back ;
  230. handles @platform_log vector_push_back ;
  231. handles @fasm_open vector_push_back ;
  232. handles @fasm_create vector_push_back ;
  233. handles @fasm_read vector_push_back ;
  234. handles @fasm_write vector_push_back ;
  235. handles @fasm_close vector_push_back ;
  236. handles @fasm_lseek vector_push_back ;
  237. handles @fasm_fatal_error vector_push_back ;
  238. handles @fasm_assembler_error vector_push_back ;
  239. handles @fasm_display_block vector_push_back ;
  240. handles @fasm_get_environment_variable vector_push_back ;
  241. handles @fasm_make_timestamp vector_push_back ;
  242. @ret_instr_enter read_ret_instr = ;
  243. "Retired instruction counter before entering fasm: " 1 platform_log ;
  244. read_ret_instr itoa 1 platform_log ;
  245. "\n" 1 platform_log ;
  246. # Enable single stepping
  247. @instr_num 0 = ;
  248. @dumping 0 = ;
  249. @breakpoint ctx "fatal_error" asmctx_get_symbol_addr = ;
  250. @breakpoint_instr_num 407500 = ;
  251. 0x10010 @single_step_handler = ;
  252. 0x1001c ** \0 ;
  253. # Run fasm
  254. $res
  255. @res handles vector_data main_addr \1 = ;
  256. # Disable single stepping
  257. 0x10014 0 = ;
  258. "Retired instruction counter after exiting fasm: " 1 platform_log ;
  259. read_ret_instr itoa 1 platform_log ;
  260. "\n" 1 platform_log ;
  261. "Executed instruction number after exiting fasm: " 1 platform_log ;
  262. instr_num itoa 1 platform_log ;
  263. "\n" 1 platform_log ;
  264. "fasm returned " 1 platform_log ;
  265. res itoa 1 platform_log ;
  266. "\n" 1 platform_log ;
  267. handles vector_destroy ;
  268. ctx asmctx_destroy ;
  269. }