corefile.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 _CORE_C_
  15. #define _CORE_C_
  16. #include "basics.h"
  17. #include "device_table.h"
  18. #include "corefile.h"
  19. typedef struct _core_mapping core_mapping;
  20. struct _core_mapping {
  21. /* common */
  22. int level;
  23. int space;
  24. unsigned_word base;
  25. unsigned_word bound;
  26. unsigned nr_bytes;
  27. /* memory map */
  28. void *free_buffer;
  29. void *buffer;
  30. /* callback map */
  31. device *device;
  32. /* growth */
  33. core_mapping *next;
  34. };
  35. struct _core_map {
  36. core_mapping *first;
  37. };
  38. typedef enum {
  39. core_read_map,
  40. core_write_map,
  41. core_execute_map,
  42. nr_core_map_types,
  43. } core_map_types;
  44. struct _core {
  45. core_map map[nr_core_map_types];
  46. };
  47. INLINE_CORE\
  48. (core *)
  49. core_create(void)
  50. {
  51. return ZALLOC(core);
  52. }
  53. INLINE_CORE\
  54. (core *)
  55. core_from_device(device *root)
  56. {
  57. root = device_root(root);
  58. ASSERT(strcmp(device_name(root), "core") == 0);
  59. return device_data(root);
  60. }
  61. INLINE_CORE\
  62. (void)
  63. core_init(core *memory)
  64. {
  65. core_map_types access_type;
  66. for (access_type = 0;
  67. access_type < nr_core_map_types;
  68. access_type++) {
  69. core_map *map = memory->map + access_type;
  70. /* blow away old mappings */
  71. core_mapping *curr = map->first;
  72. while (curr != NULL) {
  73. core_mapping *tbd = curr;
  74. curr = curr->next;
  75. if (tbd->free_buffer != NULL) {
  76. ASSERT(tbd->buffer != NULL);
  77. free(tbd->free_buffer);
  78. }
  79. free(tbd);
  80. }
  81. map->first = NULL;
  82. }
  83. }
  84. /* the core has three sub mappings that the more efficient
  85. read/write fixed quantity functions use */
  86. INLINE_CORE\
  87. (core_map *)
  88. core_readable(core *memory)
  89. {
  90. return memory->map + core_read_map;
  91. }
  92. INLINE_CORE\
  93. (core_map *)
  94. core_writeable(core *memory)
  95. {
  96. return memory->map + core_write_map;
  97. }
  98. INLINE_CORE\
  99. (core_map *)
  100. core_executable(core *memory)
  101. {
  102. return memory->map + core_execute_map;
  103. }
  104. STATIC_INLINE_CORE\
  105. (core_mapping *)
  106. new_core_mapping(attach_type attach,
  107. int space,
  108. unsigned_word addr,
  109. unsigned nr_bytes,
  110. device *device,
  111. void *buffer,
  112. void *free_buffer)
  113. {
  114. core_mapping *new_mapping = ZALLOC(core_mapping);
  115. /* common */
  116. new_mapping->level = attach;
  117. new_mapping->space = space;
  118. new_mapping->base = addr;
  119. new_mapping->nr_bytes = nr_bytes;
  120. new_mapping->bound = addr + (nr_bytes - 1);
  121. if (attach == attach_raw_memory) {
  122. new_mapping->buffer = buffer;
  123. new_mapping->free_buffer = free_buffer;
  124. }
  125. else if (attach >= attach_callback) {
  126. new_mapping->device = device;
  127. }
  128. else {
  129. error("new_core_mapping() - internal error - unknown attach type %d\n",
  130. attach);
  131. }
  132. return new_mapping;
  133. }
  134. STATIC_INLINE_CORE\
  135. (void)
  136. core_map_attach(core_map *access_map,
  137. attach_type attach,
  138. int space,
  139. unsigned_word addr,
  140. unsigned nr_bytes, /* host limited */
  141. device *client, /*callback/default*/
  142. void *buffer, /*raw_memory*/
  143. void *free_buffer) /*raw_memory*/
  144. {
  145. /* find the insertion point for this additional mapping and insert */
  146. core_mapping *next_mapping;
  147. core_mapping **last_mapping;
  148. /* actually do occasionally get a zero size map */
  149. if (nr_bytes == 0) {
  150. device_error(client, "called on core_map_attach() with size zero");
  151. }
  152. /* find the insertion point (between last/next) */
  153. next_mapping = access_map->first;
  154. last_mapping = &access_map->first;
  155. while(next_mapping != NULL
  156. && (next_mapping->level < attach
  157. || (next_mapping->level == attach
  158. && next_mapping->bound < addr))) {
  159. /* provided levels are the same */
  160. /* assert: next_mapping->base > all bases before next_mapping */
  161. /* assert: next_mapping->bound >= all bounds before next_mapping */
  162. last_mapping = &next_mapping->next;
  163. next_mapping = next_mapping->next;
  164. }
  165. /* check insertion point correct */
  166. ASSERT(next_mapping == NULL || next_mapping->level >= attach);
  167. if (next_mapping != NULL && next_mapping->level == attach
  168. && next_mapping->base < (addr + (nr_bytes - 1))) {
  169. device_error(client, "map overlap when attaching %d:0x%lx (%ld)",
  170. space, (long)addr, (long)nr_bytes);
  171. }
  172. /* create/insert the new mapping */
  173. *last_mapping = new_core_mapping(attach,
  174. space, addr, nr_bytes,
  175. client, buffer, free_buffer);
  176. (*last_mapping)->next = next_mapping;
  177. }
  178. INLINE_CORE\
  179. (void)
  180. core_attach(core *memory,
  181. attach_type attach,
  182. int space,
  183. access_type access,
  184. unsigned_word addr,
  185. unsigned nr_bytes, /* host limited */
  186. device *client) /*callback/default*/
  187. {
  188. core_map_types access_map;
  189. void *buffer;
  190. void *free_buffer;
  191. if (attach == attach_raw_memory) {
  192. /* Padd out the raw buffer to ensure that ADDR starts on a
  193. correctly aligned boundary */
  194. int padding = (addr % sizeof (unsigned64));
  195. free_buffer = zalloc(nr_bytes + padding);
  196. buffer = (char*)free_buffer + padding;
  197. }
  198. else {
  199. buffer = NULL;
  200. free_buffer = &buffer; /* marker for assertion */
  201. }
  202. for (access_map = 0;
  203. access_map < nr_core_map_types;
  204. access_map++) {
  205. switch (access_map) {
  206. case core_read_map:
  207. if (access & access_read)
  208. core_map_attach(memory->map + access_map,
  209. attach,
  210. space, addr, nr_bytes,
  211. client, buffer, free_buffer);
  212. free_buffer = NULL;
  213. break;
  214. case core_write_map:
  215. if (access & access_write)
  216. core_map_attach(memory->map + access_map,
  217. attach,
  218. space, addr, nr_bytes,
  219. client, buffer, free_buffer);
  220. free_buffer = NULL;
  221. break;
  222. case core_execute_map:
  223. if (access & access_exec)
  224. core_map_attach(memory->map + access_map,
  225. attach,
  226. space, addr, nr_bytes,
  227. client, buffer, free_buffer);
  228. free_buffer = NULL;
  229. break;
  230. default:
  231. error("core_attach() internal error\n");
  232. break;
  233. }
  234. }
  235. /* allocated buffer must attach to at least one thing */
  236. ASSERT(free_buffer == NULL);
  237. }
  238. STATIC_INLINE_CORE\
  239. (core_mapping *)
  240. core_map_find_mapping(core_map *map,
  241. unsigned_word addr,
  242. unsigned nr_bytes,
  243. cpu *processor,
  244. unsigned_word cia,
  245. int abort) /*either 0 or 1 - helps inline */
  246. {
  247. core_mapping *mapping = map->first;
  248. ASSERT((addr & (nr_bytes - 1)) == 0); /* must be aligned */
  249. ASSERT((addr + (nr_bytes - 1)) >= addr); /* must not wrap */
  250. while (mapping != NULL) {
  251. if (addr >= mapping->base
  252. && (addr + (nr_bytes - 1)) <= mapping->bound)
  253. return mapping;
  254. mapping = mapping->next;
  255. }
  256. if (abort)
  257. error("core_find_mapping() - access to unmaped address, attach a default map to handle this - addr=0x%x nr_bytes=0x%x processor=0x%x cia=0x%x\n",
  258. addr, nr_bytes, processor, cia);
  259. return NULL;
  260. }
  261. STATIC_INLINE_CORE\
  262. (void *)
  263. core_translate(core_mapping *mapping,
  264. unsigned_word addr)
  265. {
  266. return (void *)(((char *)mapping->buffer) + addr - mapping->base);
  267. }
  268. INLINE_CORE\
  269. (unsigned)
  270. core_map_read_buffer(core_map *map,
  271. void *buffer,
  272. unsigned_word addr,
  273. unsigned len)
  274. {
  275. unsigned count = 0;
  276. while (count < len) {
  277. unsigned_word raddr = addr + count;
  278. core_mapping *mapping =
  279. core_map_find_mapping(map,
  280. raddr, 1,
  281. NULL, /*processor*/
  282. 0, /*cia*/
  283. 0); /*dont-abort*/
  284. if (mapping == NULL)
  285. break;
  286. if (mapping->device != NULL) {
  287. int nr_bytes = len - count;
  288. if (raddr + nr_bytes - 1> mapping->bound)
  289. nr_bytes = mapping->bound - raddr + 1;
  290. if (device_io_read_buffer(mapping->device,
  291. (unsigned_1*)buffer + count,
  292. mapping->space,
  293. raddr,
  294. nr_bytes,
  295. 0, /*processor*/
  296. 0 /*cpu*/) != nr_bytes)
  297. break;
  298. count += nr_bytes;
  299. }
  300. else {
  301. ((unsigned_1*)buffer)[count] =
  302. *(unsigned_1*)core_translate(mapping, raddr);
  303. count += 1;
  304. }
  305. }
  306. return count;
  307. }
  308. INLINE_CORE\
  309. (unsigned)
  310. core_map_write_buffer(core_map *map,
  311. const void *buffer,
  312. unsigned_word addr,
  313. unsigned len)
  314. {
  315. unsigned count = 0;
  316. while (count < len) {
  317. unsigned_word raddr = addr + count;
  318. core_mapping *mapping = core_map_find_mapping(map,
  319. raddr, 1,
  320. NULL, /*processor*/
  321. 0, /*cia*/
  322. 0); /*dont-abort*/
  323. if (mapping == NULL)
  324. break;
  325. if (mapping->device != NULL) {
  326. int nr_bytes = len - count;
  327. if (raddr + nr_bytes - 1 > mapping->bound)
  328. nr_bytes = mapping->bound - raddr + 1;
  329. if (device_io_write_buffer(mapping->device,
  330. (unsigned_1*)buffer + count,
  331. mapping->space,
  332. raddr,
  333. nr_bytes,
  334. 0, /*processor*/
  335. 0 /*cpu*/) != nr_bytes)
  336. break;
  337. count += nr_bytes;
  338. }
  339. else {
  340. *(unsigned_1*)core_translate(mapping, raddr) =
  341. ((unsigned_1*)buffer)[count];
  342. count += 1;
  343. }
  344. }
  345. return count;
  346. }
  347. /* define the read/write 1/2/4/8/word functions */
  348. #define N 1
  349. #include "corefile-n.h"
  350. #undef N
  351. #define N 2
  352. #include "corefile-n.h"
  353. #undef N
  354. #define N 4
  355. #include "corefile-n.h"
  356. #undef N
  357. #define N 8
  358. #include "corefile-n.h"
  359. #undef N
  360. #define N word
  361. #include "corefile-n.h"
  362. #undef N
  363. #endif /* _CORE_C_ */