hw_vm.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* This file is part of the program psim.
  2. Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #ifndef _HW_VM_C_
  15. #define _HW_VM_C_
  16. #include "device_table.h"
  17. #include "cpu.h"
  18. #include <signal.h>
  19. /* DEVICE
  20. vm - virtual memory device for user simulation modes
  21. DESCRIPTION
  22. In user mode, mapped text, data and stack addresses are managed by
  23. the core. Unmapped addresses are passed onto this device (because
  24. it establishes its self as the fallback device) for processing.
  25. During initialization, children of this device will request the
  26. mapping of the initial text and data segments. Those requests are
  27. passed onto the core device so that that may establish the initial
  28. memory regions.
  29. Once the simulation has started (as noted above) any access to an
  30. unmapped address range will be passed down to this device as an IO
  31. access. This device will then either attach additional memory to
  32. the core device or signal the access as being invalid.
  33. The IOCTL function is used to notify this device of any changes to
  34. the users `brk' point.
  35. PROPERTIES
  36. stack-base = <number>
  37. Specifies the lower address of the stack segment in the users
  38. virtual address space. The initial stack page is defined by
  39. stack-base + nr-bytes.
  40. nr-bytes = <number>
  41. Specifies the maximum size of the stack segment in the users
  42. address space.
  43. */
  44. typedef struct _hw_vm_device {
  45. /* area of memory valid for stack addresses */
  46. unsigned_word stack_base; /* min possible stack value */
  47. unsigned_word stack_bound;
  48. unsigned_word stack_lower_limit;
  49. /* area of memory valid for heap addresses */
  50. unsigned_word heap_base;
  51. unsigned_word heap_bound;
  52. unsigned_word heap_upper_limit;
  53. } hw_vm_device;
  54. static void
  55. hw_vm_init_address_callback(device *me)
  56. {
  57. hw_vm_device *vm = (hw_vm_device*)device_data(me);
  58. /* revert the stack/heap variables to their defaults */
  59. vm->stack_base = device_find_integer_property(me, "stack-base");
  60. vm->stack_bound = (vm->stack_base
  61. + device_find_integer_property(me, "nr-bytes"));
  62. vm->stack_lower_limit = vm->stack_bound;
  63. vm->heap_base = 0;
  64. vm->heap_bound = 0;
  65. vm->heap_upper_limit = 0;
  66. /* establish this device as the default memory handler */
  67. device_attach_address(device_parent(me),
  68. attach_callback + 1,
  69. 0 /*address space - ignore*/,
  70. 0 /*addr - ignore*/,
  71. (((unsigned)0)-1) /*nr_bytes - ignore*/,
  72. access_read_write /*access*/,
  73. me);
  74. }
  75. static void
  76. hw_vm_attach_address(device *me,
  77. attach_type attach,
  78. int space,
  79. unsigned_word addr,
  80. unsigned nr_bytes,
  81. access_type access,
  82. device *client) /*callback/default*/
  83. {
  84. hw_vm_device *vm = (hw_vm_device*)device_data(me);
  85. /* update end of bss if necessary */
  86. if (vm->heap_base < addr + nr_bytes) {
  87. vm->heap_base = addr + nr_bytes;
  88. vm->heap_bound = addr + nr_bytes;
  89. vm->heap_upper_limit = addr + nr_bytes;
  90. }
  91. device_attach_address(device_parent(me),
  92. attach_raw_memory,
  93. 0 /*address space*/,
  94. addr,
  95. nr_bytes,
  96. access,
  97. me);
  98. }
  99. static unsigned
  100. hw_vm_add_space(device *me,
  101. unsigned_word addr,
  102. unsigned nr_bytes,
  103. cpu *processor,
  104. unsigned_word cia)
  105. {
  106. hw_vm_device *vm = (hw_vm_device*)device_data(me);
  107. unsigned_word block_addr;
  108. unsigned block_nr_bytes;
  109. /* an address in the stack area, allocate just down to the addressed
  110. page */
  111. if (addr >= vm->stack_base && addr < vm->stack_lower_limit) {
  112. block_addr = FLOOR_PAGE(addr);
  113. block_nr_bytes = vm->stack_lower_limit - block_addr;
  114. vm->stack_lower_limit = block_addr;
  115. }
  116. /* an address in the heap area, allocate all of the required heap */
  117. else if (addr >= vm->heap_upper_limit && addr < vm->heap_bound) {
  118. block_addr = vm->heap_upper_limit;
  119. block_nr_bytes = vm->heap_bound - vm->heap_upper_limit;
  120. vm->heap_upper_limit = vm->heap_bound;
  121. }
  122. /* oops - an invalid address - abort the cpu */
  123. else if (processor != NULL) {
  124. cpu_halt(processor, cia, was_signalled, SIGSEGV);
  125. return 0;
  126. }
  127. /* 2*oops - an invalid address and no processor */
  128. else {
  129. return 0;
  130. }
  131. /* got the parameters, allocate the space */
  132. device_attach_address(device_parent(me),
  133. attach_raw_memory,
  134. 0 /*address space*/,
  135. block_addr,
  136. block_nr_bytes,
  137. access_read_write,
  138. me);
  139. return block_nr_bytes;
  140. }
  141. static unsigned
  142. hw_vm_io_read_buffer_callback(device *me,
  143. void *dest,
  144. int space,
  145. unsigned_word addr,
  146. unsigned nr_bytes,
  147. cpu *processor,
  148. unsigned_word cia)
  149. {
  150. if (hw_vm_add_space(me, addr, nr_bytes, processor, cia) >= nr_bytes) {
  151. memset(dest, 0, nr_bytes); /* always initialized to zero */
  152. return nr_bytes;
  153. }
  154. else
  155. return 0;
  156. }
  157. static unsigned
  158. hw_vm_io_write_buffer_callback(device *me,
  159. const void *source,
  160. int space,
  161. unsigned_word addr,
  162. unsigned nr_bytes,
  163. cpu *processor,
  164. unsigned_word cia)
  165. {
  166. if (hw_vm_add_space(me, addr, nr_bytes, processor, cia) >= nr_bytes) {
  167. return device_dma_write_buffer(device_parent(me), source,
  168. space, addr,
  169. nr_bytes,
  170. 0/*violate_read_only*/);
  171. }
  172. else
  173. return 0;
  174. }
  175. static int
  176. hw_vm_ioctl(device *me,
  177. cpu *processor,
  178. unsigned_word cia,
  179. device_ioctl_request request,
  180. va_list ap)
  181. {
  182. /* While the caller is notified that the heap has grown by the
  183. requested amount, the heap is actually extended out to a page
  184. boundary. */
  185. hw_vm_device *vm = (hw_vm_device*)device_data(me);
  186. switch (request) {
  187. case device_ioctl_break:
  188. {
  189. unsigned_word requested_break = va_arg(ap, unsigned_word);
  190. unsigned_word new_break = ALIGN_8(requested_break);
  191. unsigned_word old_break = vm->heap_bound;
  192. signed_word delta = new_break - old_break;
  193. if (delta > 0)
  194. vm->heap_bound = ALIGN_PAGE(new_break);
  195. break;
  196. }
  197. default:
  198. device_error(me, "Unsupported ioctl request");
  199. break;
  200. }
  201. return 0;
  202. }
  203. static device_callbacks const hw_vm_callbacks = {
  204. { hw_vm_init_address_callback, },
  205. { hw_vm_attach_address,
  206. passthrough_device_address_detach, },
  207. { hw_vm_io_read_buffer_callback,
  208. hw_vm_io_write_buffer_callback, },
  209. { NULL, passthrough_device_dma_write_buffer, },
  210. { NULL, }, /* interrupt */
  211. { generic_device_unit_decode,
  212. generic_device_unit_encode, },
  213. NULL, /* instance */
  214. hw_vm_ioctl,
  215. };
  216. static void *
  217. hw_vm_create(const char *name,
  218. const device_unit *address,
  219. const char *args)
  220. {
  221. hw_vm_device *vm = ZALLOC(hw_vm_device);
  222. return vm;
  223. }
  224. const device_descriptor hw_vm_device_descriptor[] = {
  225. { "vm", hw_vm_create, &hw_vm_callbacks },
  226. { NULL },
  227. };
  228. #endif /* _HW_VM_C_ */