5i.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <mach.h>
  5. #include "arm.h"
  6. #include <tos.h>
  7. char* file = "5.out";
  8. int datasize;
  9. ulong textbase;
  10. Biobuf bp, bi;
  11. Fhdr fhdr;
  12. void
  13. main(int argc, char **argv)
  14. {
  15. argc--;
  16. argv++;
  17. bioout = &bp;
  18. bin = &bi;
  19. Binit(bioout, 1, OWRITE);
  20. Binit(bin, 0, OREAD);
  21. tlb.on = 1;
  22. tlb.tlbsize = 24;
  23. if(argc)
  24. file = argv[0];
  25. argc--;
  26. argv++;
  27. text = open(file, OREAD);
  28. if(text < 0)
  29. fatal(1, "open text '%s'", file);
  30. Bprint(bioout, "5i\n");
  31. inithdr(text);
  32. initstk(argc, argv);
  33. cmd();
  34. }
  35. void
  36. initmap()
  37. {
  38. ulong t, d, b, bssend;
  39. Segment *s;
  40. t = (fhdr.txtaddr+fhdr.txtsz+(BY2PG-1)) & ~(BY2PG-1);
  41. d = (t + fhdr.datsz + (BY2PG-1)) & ~(BY2PG-1);
  42. bssend = t + fhdr.datsz + fhdr.bsssz;
  43. b = (bssend + (BY2PG-1)) & ~(BY2PG-1);
  44. s = &memory.seg[Text];
  45. s->type = Text;
  46. s->base = fhdr.txtaddr - fhdr.hdrsz;
  47. s->end = t;
  48. s->fileoff = fhdr.txtoff - fhdr.hdrsz;
  49. s->fileend = s->fileoff + fhdr.txtsz;
  50. s->table = emalloc(((s->end-s->base)/BY2PG)*sizeof(uchar*));
  51. iprof = emalloc(((s->end-s->base)/PROFGRAN)*sizeof(long));
  52. textbase = s->base;
  53. s = &memory.seg[Data];
  54. s->type = Data;
  55. s->base = t;
  56. s->end = t+(d-t);
  57. s->fileoff = fhdr.datoff;
  58. s->fileend = s->fileoff + fhdr.datsz;
  59. datasize = fhdr.datsz;
  60. s->table = emalloc(((s->end-s->base)/BY2PG)*sizeof(uchar*));
  61. s = &memory.seg[Bss];
  62. s->type = Bss;
  63. s->base = d;
  64. s->end = d+(b-d);
  65. s->table = emalloc(((s->end-s->base)/BY2PG)*sizeof(uchar*));
  66. s = &memory.seg[Stack];
  67. s->type = Stack;
  68. s->base = STACKTOP-STACKSIZE;
  69. s->end = STACKTOP;
  70. s->table = emalloc(((s->end-s->base)/BY2PG)*sizeof(uchar*));
  71. reg.r[REGPC] = fhdr.entry;
  72. }
  73. void
  74. inithdr(int fd)
  75. {
  76. Symbol s;
  77. extern Machdata armmach;
  78. seek(fd, 0, 0);
  79. if (!crackhdr(fd, &fhdr))
  80. fatal(0, "read text header");
  81. if(fhdr.type != FARM )
  82. fatal(0, "bad magic number: %d %d", fhdr.type, FARM);
  83. if (syminit(fd, &fhdr) < 0)
  84. fatal(0, "%r\n");
  85. symmap = loadmap(symmap, fd, &fhdr);
  86. if (mach->sbreg && lookup(0, mach->sbreg, &s))
  87. mach->sb = s.value;
  88. machdata = &armmach;
  89. }
  90. void
  91. reset(void)
  92. {
  93. int i, l, m;
  94. Segment *s;
  95. Breakpoint *b;
  96. memset(&reg, 0, sizeof(Registers));
  97. for(i = 0; i > Nseg; i++) {
  98. s = &memory.seg[i];
  99. l = ((s->end-s->base)/BY2PG)*sizeof(uchar*);
  100. for(m = 0; m < l; m++)
  101. if(s->table[m])
  102. free(s->table[m]);
  103. free(s->table);
  104. }
  105. free(iprof);
  106. memset(&memory, 0, sizeof(memory));
  107. for(b = bplist; b; b = b->next)
  108. b->done = b->count;
  109. }
  110. void
  111. initstk(int argc, char *argv[])
  112. {
  113. ulong size;
  114. ulong sp, ap, tos;
  115. int i;
  116. char *p;
  117. initmap();
  118. tos = STACKTOP - sizeof(Tos)*2; /* we'll assume twice the host's is big enough */
  119. sp = tos;
  120. /* Build exec stack */
  121. size = strlen(file)+1+BY2WD+BY2WD+BY2WD;
  122. for(i = 0; i < argc; i++)
  123. size += strlen(argv[i])+BY2WD+1;
  124. sp -= size;
  125. sp &= ~7;
  126. reg.r[0] = tos;
  127. reg.r[13] = sp;
  128. reg.r[1] = STACKTOP-4; /* Plan 9 profiling clock */
  129. /* Push argc */
  130. putmem_w(sp, argc+1);
  131. sp += BY2WD;
  132. /* Compute sizeof(argv) and push argv[0] */
  133. ap = sp+((argc+1)*BY2WD)+BY2WD;
  134. putmem_w(sp, ap);
  135. sp += BY2WD;
  136. /* Build argv[0] string into stack */
  137. for(p = file; *p; p++)
  138. putmem_b(ap++, *p);
  139. putmem_b(ap++, '\0');
  140. /* Loop through pushing the arguments */
  141. for(i = 0; i < argc; i++) {
  142. putmem_w(sp, ap);
  143. sp += BY2WD;
  144. for(p = argv[i]; *p; p++)
  145. putmem_b(ap++, *p);
  146. putmem_b(ap++, '\0');
  147. }
  148. /* Null terminate argv */
  149. putmem_w(sp, 0);
  150. }
  151. void
  152. fatal(int syserr, char *fmt, ...)
  153. {
  154. char buf[ERRMAX], *s;
  155. va_list arg;
  156. va_start(arg, fmt);
  157. vseprint(buf, buf+sizeof(buf), fmt, arg);
  158. va_end(arg);
  159. s = "5i: %s\n";
  160. if(syserr)
  161. s = "5i: %s: %r\n";
  162. fprint(2, s, buf);
  163. exits(buf);
  164. }
  165. void
  166. itrace(char *fmt, ...)
  167. {
  168. char buf[128];
  169. va_list arg;
  170. va_start(arg, fmt);
  171. vseprint(buf, buf+sizeof(buf), fmt, arg);
  172. va_end(arg);
  173. Bprint(bioout, "%8lux %.8lux %2d %s\n", reg.ar, reg.ir, reg.class, buf);
  174. }
  175. void
  176. dumpreg(void)
  177. {
  178. int i;
  179. Bprint(bioout, "PC #%-8lux SP #%-8lux \n",
  180. reg.r[REGPC], reg.r[REGSP]);
  181. for(i = 0; i < 16; i++) {
  182. if((i%4) == 0 && i != 0)
  183. Bprint(bioout, "\n");
  184. Bprint(bioout, "R%-2d #%-8lux ", i, reg.r[i]);
  185. }
  186. Bprint(bioout, "\n");
  187. }
  188. void
  189. dumpfreg(void)
  190. {
  191. }
  192. void
  193. dumpdreg(void)
  194. {
  195. }
  196. void *
  197. emalloc(ulong size)
  198. {
  199. void *a;
  200. a = malloc(size);
  201. if(a == 0)
  202. fatal(0, "no memory");
  203. memset(a, 0, size);
  204. return a;
  205. }
  206. void *
  207. erealloc(void *a, ulong oldsize, ulong size)
  208. {
  209. void *n;
  210. n = malloc(size);
  211. if(n == 0)
  212. fatal(0, "no memory");
  213. memset(n, 0, size);
  214. if(size > oldsize)
  215. size = oldsize;
  216. memmove(n, a, size);
  217. return n;
  218. }