ramfs.g 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. const RAMFILE_SIZE 0
  15. const RAMFILE_DATA 4
  16. const SIZEOF_RAMFILE 8
  17. fun ramfile_init 0 {
  18. $file
  19. @file SIZEOF_RAMFILE malloc = ;
  20. file RAMFILE_SIZE take_addr 0 = ;
  21. file RAMFILE_DATA take_addr 4 vector_init = ;
  22. file ret ;
  23. }
  24. fun ramfile_destroy 1 {
  25. $file
  26. @file 0 param = ;
  27. file RAMFILE_DATA take vector_destroy ;
  28. file free ;
  29. }
  30. fun ramfile_truncate 1 {
  31. $file
  32. @file 0 param = ;
  33. file RAMFILE_DATA take vector_destroy ;
  34. file RAMFILE_DATA take_addr 4 vector_init = ;
  35. file RAMFILE_SIZE take_addr 0 = ;
  36. }
  37. fun ramfile_read 2 {
  38. $file
  39. $pos
  40. @file 1 param = ;
  41. @pos 0 param = ;
  42. pos file RAMFILE_SIZE take <= "ramfile_read: invalid read position" assert_msg ;
  43. if pos file RAMFILE_SIZE take == {
  44. 0xffffffff ret ;
  45. }
  46. pos file RAMFILE_DATA take vector_size 4 * < "ramfile_read: error 1" assert_msg ;
  47. file RAMFILE_DATA take vector_data pos + **c ret ;
  48. }
  49. fun ramfile_write 3 {
  50. $file
  51. $pos
  52. $c
  53. @file 2 param = ;
  54. @pos 1 param = ;
  55. @c 0 param = ;
  56. $size
  57. $data
  58. @size file RAMFILE_SIZE take = ;
  59. @data file RAMFILE_DATA take = ;
  60. pos size <= "ramfile_write: invalid write position" assert_msg ;
  61. if pos size == {
  62. @size size 1 + = ;
  63. file RAMFILE_SIZE take_addr size = ;
  64. if pos data vector_size 4 * == {
  65. data 0 vector_push_back ;
  66. }
  67. }
  68. # "writing " log ;
  69. # c itoa log ;
  70. # " pos = " log ;
  71. # pos itoa log ;
  72. # " size = " log ;
  73. # size itoa log ;
  74. # "\n" log ;
  75. pos file RAMFILE_SIZE take < "ramfile_write: error 1" assert_msg ;
  76. pos file RAMFILE_DATA take vector_size 4 * < "ramfile_write: error 2" assert_msg ;
  77. file RAMFILE_DATA take vector_data pos + c =c ;
  78. }
  79. const RAMFD_DESTROY 0
  80. const RAMFD_READ 4
  81. const RAMFD_WRITE 8
  82. const RAMFD_TRUNCATE 12
  83. const RAMFD_SEEK 16
  84. const RAMFD_FILE 20
  85. const RAMFD_POS 24
  86. const SIZEOF_RAMFD 28
  87. fun ramfd_destroy 1 {
  88. $fd
  89. @fd 0 param = ;
  90. fd free ;
  91. }
  92. fun ramfd_read 1 {
  93. $fd
  94. @fd 0 param = ;
  95. $file
  96. $pos
  97. @file fd RAMFD_FILE take = ;
  98. @pos fd RAMFD_POS take = ;
  99. $c
  100. @c file pos ramfile_read = ;
  101. # "reading " log ;
  102. # c itoa log ;
  103. # " at " log ;
  104. # pos itoa log ;
  105. # "\n" log ;
  106. fd RAMFD_POS take_addr pos 1 + = ;
  107. c ret ;
  108. }
  109. fun ramfd_write 2 {
  110. $fd
  111. $c
  112. @fd 1 param = ;
  113. @c 0 param = ;
  114. $file
  115. $pos
  116. @file fd RAMFD_FILE take = ;
  117. @pos fd RAMFD_POS take = ;
  118. file pos c ramfile_write ;
  119. fd RAMFD_POS take_addr pos 1 + = ;
  120. }
  121. fun ramfd_truncate 1 {
  122. $fd
  123. @fd 0 param = ;
  124. $file
  125. @file fd RAMFD_FILE take = ;
  126. file ramfile_truncate ;
  127. }
  128. fun ramfd_seek 3 {
  129. $fd
  130. $off
  131. $whence
  132. @fd 2 param = ;
  133. @off 1 param = ;
  134. @whence 0 param = ;
  135. $size
  136. @size fd RAMFD_FILE take RAMFILE_SIZE take = ;
  137. if whence 1 == {
  138. @off off fd RAMFD_POS take + = ;
  139. }
  140. if whence 2 == {
  141. @off off size + = ;
  142. }
  143. if off size > {
  144. @off size = ;
  145. }
  146. # "seeking at " log ;
  147. # off itoa log ;
  148. # "\n" log ;
  149. fd RAMFD_POS take_addr off = ;
  150. off ret ;
  151. }
  152. fun ramfd_init 1 {
  153. $file
  154. @file 0 param = ;
  155. $fd
  156. @fd SIZEOF_RAMFD malloc = ;
  157. fd RAMFD_DESTROY take_addr @ramfd_destroy = ;
  158. fd RAMFD_READ take_addr @ramfd_read = ;
  159. fd RAMFD_WRITE take_addr @ramfd_write = ;
  160. fd RAMFD_TRUNCATE take_addr @ramfd_truncate = ;
  161. fd RAMFD_SEEK take_addr @ramfd_seek = ;
  162. fd RAMFD_FILE take_addr file = ;
  163. fd RAMFD_POS take_addr 0 = ;
  164. fd ret ;
  165. }
  166. const RAMMOUNT_DESTROY 0
  167. const RAMMOUNT_OPEN 4
  168. const RAMMOUNT_FILES 8
  169. const SIZEOF_RAMMOUNT 12
  170. fun ramfile_destroy_closure 3 {
  171. $ctx
  172. $key
  173. $value
  174. @ctx 2 param = ;
  175. @key 1 param = ;
  176. @value 0 param = ;
  177. value ramfile_destroy ;
  178. }
  179. fun rammount_destroy 1 {
  180. $ram
  181. @ram 0 param = ;
  182. $files
  183. @files ram RAMMOUNT_FILES take = ;
  184. files @ramfile_destroy_closure 0 map_foreach ;
  185. files map_destroy ;
  186. ram free ;
  187. }
  188. fun rammount_open 2 {
  189. $mount
  190. $name
  191. @mount 1 param = ;
  192. @name 0 param = ;
  193. $files
  194. @files mount RAMMOUNT_FILES take = ;
  195. $file
  196. if files name map_has ! {
  197. @file ramfile_init = ;
  198. files name file map_set ;
  199. } else {
  200. @file files name map_at = ;
  201. }
  202. $fd
  203. @fd file ramfd_init = ;
  204. fd ret ;
  205. }
  206. fun rammount_init 0 {
  207. $ram
  208. @ram SIZEOF_RAMMOUNT malloc = ;
  209. ram RAMMOUNT_DESTROY take_addr @rammount_destroy = ;
  210. ram RAMMOUNT_OPEN take_addr @rammount_open = ;
  211. ram RAMMOUNT_FILES take_addr map_init = ;
  212. ram ret ;
  213. }