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