emul_generic.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* This file is part of the program psim.
  2. Copyright (C) 1994-1997, 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 _EMUL_GENERIC_C_
  15. #define _EMUL_GENERIC_C_
  16. #include "emul_generic.h"
  17. #ifndef STATIC_INLINE_EMUL_GENERIC
  18. #define STATIC_INLINE_EMUL_GENERIC STATIC_INLINE
  19. #endif
  20. STATIC_INLINE_EMUL_GENERIC void
  21. emul_syscall_enter(emul_syscall *emul,
  22. int call,
  23. int arg0,
  24. cpu *processor,
  25. unsigned_word cia)
  26. {
  27. printf_filtered("%d:0x%lx:%s(",
  28. cpu_nr(processor) + 1,
  29. (long)cia,
  30. emul->syscall_descriptor[call].name);
  31. }
  32. STATIC_INLINE_EMUL_GENERIC void
  33. emul_syscall_exit(emul_syscall *emul,
  34. int call,
  35. int arg0,
  36. cpu *processor,
  37. unsigned_word cia)
  38. {
  39. int status = cpu_registers(processor)->gpr[3];
  40. int error = cpu_registers(processor)->gpr[0];
  41. printf_filtered(")=%d", status);
  42. if (error > 0 && error < emul->nr_error_names)
  43. printf_filtered("[%s]", emul->error_names[error]);
  44. printf_filtered("\n");
  45. }
  46. INLINE_EMUL_GENERIC unsigned64
  47. emul_read_gpr64(cpu *processor,
  48. int g)
  49. {
  50. unsigned32 hi;
  51. unsigned32 lo;
  52. if (CURRENT_TARGET_BYTE_ORDER == BIG_ENDIAN) {
  53. hi = cpu_registers(processor)->gpr[g];
  54. lo = cpu_registers(processor)->gpr[g+1];
  55. }
  56. else {
  57. lo = cpu_registers(processor)->gpr[g];
  58. hi = cpu_registers(processor)->gpr[g+1];
  59. }
  60. return (INSERTED64(hi, 0, 31) | INSERTED64(lo, 32, 63));
  61. }
  62. INLINE_EMUL_GENERIC void
  63. emul_write_gpr64(cpu *processor,
  64. int g,
  65. unsigned64 val)
  66. {
  67. unsigned32 hi = EXTRACTED64(val, 0, 31);
  68. unsigned32 lo = EXTRACTED64(val, 32, 63);
  69. if (CURRENT_TARGET_BYTE_ORDER == BIG_ENDIAN) {
  70. cpu_registers(processor)->gpr[g] = hi;
  71. cpu_registers(processor)->gpr[g+1] = lo;
  72. }
  73. else {
  74. cpu_registers(processor)->gpr[g] = lo;
  75. cpu_registers(processor)->gpr[g+1] = hi;
  76. }
  77. }
  78. INLINE_EMUL_GENERIC char *
  79. emul_read_string(char *dest,
  80. unsigned_word addr,
  81. unsigned nr_bytes,
  82. cpu *processor,
  83. unsigned_word cia)
  84. {
  85. unsigned nr_moved = 0;
  86. if (addr == 0)
  87. return NULL;
  88. while (1) {
  89. dest[nr_moved] = vm_data_map_read_1(cpu_data_map(processor),
  90. addr + nr_moved,
  91. processor, cia);
  92. if (dest[nr_moved] == '\0' || nr_moved >= nr_bytes)
  93. break;
  94. nr_moved++;
  95. }
  96. dest[nr_moved] = '\0';
  97. return dest;
  98. }
  99. INLINE_EMUL_GENERIC void
  100. emul_write_status(cpu *processor,
  101. int status,
  102. int errno)
  103. {
  104. if (status == -1 && errno != 0) {
  105. cpu_registers(processor)->gpr[3] = errno;
  106. CR_SET(0, cr_i_summary_overflow);
  107. }
  108. else {
  109. cpu_registers(processor)->gpr[3] = status;
  110. CR_SET(0, 0);
  111. }
  112. }
  113. INLINE_EMUL_GENERIC void
  114. emul_write2_status(cpu *processor,
  115. int status1,
  116. int status2,
  117. int errno)
  118. {
  119. if (status1 == -1 && errno != 0) {
  120. cpu_registers(processor)->gpr[3] = errno;
  121. CR_SET(0, cr_i_summary_overflow);
  122. }
  123. else {
  124. cpu_registers(processor)->gpr[3] = status1;
  125. cpu_registers(processor)->gpr[4] = status2;
  126. CR_SET(0, 0);
  127. }
  128. }
  129. INLINE_EMUL_GENERIC unsigned_word
  130. emul_read_word(unsigned_word addr,
  131. cpu *processor,
  132. unsigned_word cia)
  133. {
  134. return vm_data_map_read_word(cpu_data_map(processor),
  135. addr,
  136. processor, cia);
  137. }
  138. INLINE_EMUL_GENERIC void
  139. emul_write_word(unsigned_word addr,
  140. unsigned_word buf,
  141. cpu *processor,
  142. unsigned_word cia)
  143. {
  144. vm_data_map_write_word(cpu_data_map(processor),
  145. addr,
  146. buf,
  147. processor, cia);
  148. }
  149. INLINE_EMUL_GENERIC void
  150. emul_write_buffer(const void *source,
  151. unsigned_word addr,
  152. unsigned nr_bytes,
  153. cpu *processor,
  154. unsigned_word cia)
  155. {
  156. int nr_moved;
  157. for (nr_moved = 0; nr_moved < nr_bytes; nr_moved++) {
  158. vm_data_map_write_1(cpu_data_map(processor),
  159. addr + nr_moved,
  160. ((const char*)source)[nr_moved],
  161. processor, cia);
  162. }
  163. }
  164. INLINE_EMUL_GENERIC void
  165. emul_read_buffer(void *dest,
  166. unsigned_word addr,
  167. unsigned nr_bytes,
  168. cpu *processor,
  169. unsigned_word cia)
  170. {
  171. int nr_moved;
  172. for (nr_moved = 0; nr_moved < nr_bytes; nr_moved++) {
  173. ((char*)dest)[nr_moved] = vm_data_map_read_1(cpu_data_map(processor),
  174. addr + nr_moved,
  175. processor, cia);
  176. }
  177. }
  178. INLINE_EMUL_GENERIC void
  179. emul_do_system_call(os_emul_data *emul_data,
  180. emul_syscall *emul,
  181. unsigned call,
  182. const int arg0,
  183. cpu *processor,
  184. unsigned_word cia)
  185. {
  186. emul_syscall_handler *handler = NULL;
  187. if (call >= emul->nr_system_calls)
  188. error("do_call() os_emul call %d out-of-range\n", call);
  189. handler = emul->syscall_descriptor[call].handler;
  190. if (handler == NULL) {
  191. if (emul->syscall_descriptor[call].name) {
  192. error("do_call() unimplemented call %s\n", emul->syscall_descriptor[call].name);
  193. } else {
  194. error("do_call() unimplemented call %d\n", call);
  195. }
  196. }
  197. if (WITH_TRACE && ppc_trace[trace_os_emul])
  198. emul_syscall_enter(emul, call, arg0, processor, cia);
  199. cpu_registers(processor)->gpr[0] = 0; /* default success */
  200. handler(emul_data, call, arg0, processor, cia);
  201. if (WITH_TRACE && ppc_trace[trace_os_emul])
  202. emul_syscall_exit(emul, call, arg0, processor, cia);
  203. }
  204. /* default size for the first bank of memory */
  205. #ifndef OEA_MEMORY_SIZE
  206. #define OEA_MEMORY_SIZE 0x100000
  207. #endif
  208. /* Add options to the device tree */
  209. INLINE_EMUL_GENERIC void
  210. emul_add_tree_options(device *tree,
  211. bfd *image,
  212. const char *emul,
  213. const char *env,
  214. int oea_interrupt_prefix)
  215. {
  216. int little_endian = 0;
  217. /* sort out little endian */
  218. if (tree_find_property(tree, "/options/little-endian?"))
  219. little_endian = tree_find_boolean_property(tree, "/options/little-endian?");
  220. else {
  221. #ifdef bfd_little_endian /* new bfd */
  222. little_endian = (image != NULL && bfd_little_endian(image));
  223. #else
  224. little_endian = (image != NULL &&
  225. !image->xvec->byteorder_big_p);
  226. #endif
  227. tree_parse(tree, "/options/little-endian? %s",
  228. little_endian ? "true" : "false");
  229. }
  230. /* misc other stuff */
  231. tree_parse(tree, "/openprom/options/oea-memory-size 0x%x",
  232. OEA_MEMORY_SIZE);
  233. tree_parse(tree, "/openprom/options/oea-interrupt-prefix %d",
  234. oea_interrupt_prefix);
  235. tree_parse(tree, "/openprom/options/smp 1");
  236. tree_parse(tree, "/openprom/options/env %s", env);
  237. tree_parse(tree, "/openprom/options/os-emul %s", emul);
  238. tree_parse(tree, "/openprom/options/strict-alignment? %s",
  239. (WITH_ALIGNMENT == STRICT_ALIGNMENT)
  240. ? "true" : "false");
  241. tree_parse(tree, "/openprom/options/floating-point? %s",
  242. WITH_FLOATING_POINT ? "true" : "false");
  243. tree_parse(tree, "/openprom/options/use-stdio? %s",
  244. ((WITH_STDIO == DO_USE_STDIO
  245. || WITH_STDIO == 0)
  246. ? "true" : "false"));
  247. tree_parse(tree, "/openprom/options/model \"%s",
  248. model_name[WITH_DEFAULT_MODEL]);
  249. tree_parse(tree, "/openprom/options/model-issue %d",
  250. MODEL_ISSUE_IGNORE);
  251. /* useful options */
  252. }
  253. INLINE_EMUL_GENERIC void
  254. emul_add_tree_hardware(device *root)
  255. {
  256. int i;
  257. int nr_cpus = tree_find_integer_property(root, "/openprom/options/smp");
  258. /* sanity check the number of processors */
  259. if (nr_cpus > MAX_NR_PROCESSORS)
  260. error("Specified number of processors (%d) exceeds the number configured (%d).\n",
  261. nr_cpus, MAX_NR_PROCESSORS);
  262. /* set the number of address cells (1 or 2) */
  263. tree_parse(root, "#address-cells %d", WITH_TARGET_WORD_BITSIZE / 32);
  264. /* add some memory */
  265. if (tree_find_device(root, "/memory") == NULL) {
  266. unsigned_word memory_size =
  267. tree_find_integer_property(root, "/openprom/options/oea-memory-size");
  268. const unsigned_word avail_start = 0x3000;
  269. tree_parse(root, "/memory@0/reg 0x0 0x%lx",
  270. (unsigned long)memory_size);
  271. /* reserve the first 0x3000 for the PowerPC interrupt table */
  272. tree_parse(root, "/memory@0/available 0x%lx 0x%lx",
  273. (unsigned long)avail_start,
  274. (unsigned long)memory_size - avail_start);
  275. }
  276. /* our processors */
  277. for (i = 0; i < nr_cpus; i++) {
  278. tree_parse(root, "/cpus/cpu@%d/cpu-nr %d", i, i);
  279. }
  280. /* the debugging pal - hide it in the openprom and don't attach it
  281. to any bus */
  282. tree_parse(root, "/openprom/pal");
  283. /* chosen etc */
  284. tree_parse(root, "/chosen/stdin */openprom/pal");
  285. tree_parse(root, "/chosen/stdout !/chosen/stdin");
  286. tree_parse(root, "/chosen/memory */memory");
  287. }
  288. #endif /* _EMUL_GENERIC_C_ */