mem.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <mach.h>
  5. #define Extern extern
  6. #include "power.h"
  7. extern ulong textbase;
  8. ulong
  9. ifetch(ulong addr)
  10. {
  11. uchar *va;
  12. ulong px;
  13. if(addr&3) {
  14. Bprint(bioout, "instruction_address_not_aligned [addr %.8lux]\n", addr);
  15. longjmp(errjmp, 0);
  16. }
  17. if(icache.on)
  18. updateicache(addr);
  19. va = vaddr(addr);
  20. px = (addr-textbase)/PROFGRAN;
  21. if(px < iprofsize)
  22. iprof[px]++;
  23. va += addr&(BY2PG-1);
  24. return va[0]<<24 | va[1]<<16 | va[2]<<8 | va[3];
  25. }
  26. ulong
  27. getmem_4(ulong addr)
  28. {
  29. ulong val;
  30. int i;
  31. val = 0;
  32. for(i = 0; i < 4; i++)
  33. val = val<<8 | getmem_b(addr++);
  34. return val;
  35. }
  36. ulong
  37. getmem_2(ulong addr)
  38. {
  39. ulong val;
  40. val = getmem_b(addr);
  41. val = val<<8 | getmem_b(addr+1);
  42. return val;
  43. }
  44. uvlong
  45. getmem_v(ulong addr)
  46. {
  47. if(addr&3) { /* 7? */
  48. Bprint(bioout, "mem_address_not_aligned [load addr %.8lux]\n", addr);
  49. longjmp(errjmp, 0);
  50. }
  51. return ((uvlong)getmem_w(addr) << 32) | getmem_w(addr+4);
  52. }
  53. ulong
  54. getmem_w(ulong addr)
  55. {
  56. uchar *va;
  57. if(addr&3) {
  58. Bprint(bioout, "mem_address_not_aligned [load addr %.8lux]\n", addr);
  59. longjmp(errjmp, 0);
  60. }
  61. if(membpt)
  62. brkchk(addr, Read);
  63. va = vaddr(addr);
  64. va += addr&(BY2PG-1);
  65. return va[0]<<24 | va[1]<<16 | va[2]<<8 | va[3];
  66. }
  67. ushort
  68. getmem_h(ulong addr)
  69. {
  70. uchar *va;
  71. if(addr&1) {
  72. Bprint(bioout, "mem_address_not_aligned [load addr %.8lux]\n", addr);
  73. longjmp(errjmp, 0);
  74. }
  75. if(membpt)
  76. brkchk(addr, Read);
  77. va = vaddr(addr);
  78. va += addr&(BY2PG-1);
  79. return va[0]<<8 | va[1];
  80. }
  81. uchar
  82. getmem_b(ulong addr)
  83. {
  84. uchar *va;
  85. if(membpt)
  86. brkchk(addr, Read);
  87. va = vaddr(addr);
  88. va += addr&(BY2PG-1);
  89. return va[0];
  90. }
  91. void
  92. putmem_v(ulong addr, uvlong data)
  93. {
  94. if(addr&3) { /* 7? */
  95. Bprint(bioout, "mem_address_not_aligned [store addr %.8lux]\n", addr);
  96. longjmp(errjmp, 0);
  97. }
  98. putmem_w(addr, data>>32); /* two stages, to catch brkchk */
  99. putmem_w(addr+4, data);
  100. }
  101. void
  102. putmem_w(ulong addr, ulong data)
  103. {
  104. uchar *va;
  105. if(addr&3) {
  106. Bprint(bioout, "mem_address_not_aligned [store addr %.8lux]\n", addr);
  107. longjmp(errjmp, 0);
  108. }
  109. va = vaddr(addr);
  110. va += addr&(BY2PG-1);
  111. va[0] = data>>24;
  112. va[1] = data>>16;
  113. va[2] = data>>8;
  114. va[3] = data;
  115. if(membpt)
  116. brkchk(addr, Write);
  117. }
  118. void
  119. putmem_b(ulong addr, uchar data)
  120. {
  121. uchar *va;
  122. va = vaddr(addr);
  123. va += addr&(BY2PG-1);
  124. va[0] = data;
  125. if(membpt)
  126. brkchk(addr, Write);
  127. }
  128. void
  129. putmem_h(ulong addr, short data)
  130. {
  131. uchar *va;
  132. if(addr&1) {
  133. Bprint(bioout, "mem_address_not_aligned [store addr %.8lux]\n", addr);
  134. longjmp(errjmp, 0);
  135. }
  136. va = vaddr(addr);
  137. va += addr&(BY2PG-1);
  138. va[0] = data>>8;
  139. va[1] = data;
  140. if(membpt)
  141. brkchk(addr, Write);
  142. }
  143. char *
  144. memio(char *mb, ulong mem, int size, int dir)
  145. {
  146. int i;
  147. char *buf, c;
  148. if(size < 0) {
  149. Bprint(bioout, "memio: invalid size: %d\n", size);
  150. longjmp(errjmp, 0);
  151. }
  152. if(mb == 0)
  153. mb = emalloc(size);
  154. buf = mb;
  155. switch(dir) {
  156. default:
  157. fatal(0, "memio");
  158. case MemRead:
  159. while(size--)
  160. *mb++ = getmem_b(mem++);
  161. break;
  162. case MemReadstring:
  163. for(;;) {
  164. if(size-- == 0) {
  165. Bprint(bioout, "memio: user/kernel copy too long for qi\n");
  166. longjmp(errjmp, 0);
  167. }
  168. c = getmem_b(mem++);
  169. *mb++ = c;
  170. if(c == '\0')
  171. break;
  172. }
  173. break;
  174. case MemWrite:
  175. for(i = 0; i < size; i++)
  176. putmem_b(mem++, *mb++);
  177. break;
  178. }
  179. return buf;
  180. }
  181. void *
  182. vaddr(ulong addr)
  183. {
  184. Segment *s, *es;
  185. int off, foff, l, n;
  186. uchar **p, *a;
  187. es = &memory.seg[Nseg];
  188. for(s = memory.seg; s < es; s++) {
  189. if(addr >= s->base && addr < s->end) {
  190. s->refs++;
  191. off = (addr-s->base)/BY2PG;
  192. p = &s->table[off];
  193. if(*p)
  194. return *p;
  195. s->rss++;
  196. switch(s->type) {
  197. default:
  198. fatal(0, "vaddr");
  199. case Text:
  200. *p = emalloc(BY2PG);
  201. if(seek(text, s->fileoff+(off*BY2PG), 0) < 0)
  202. fatal(1, "vaddr text seek");
  203. if(read(text, *p, BY2PG) < 0)
  204. fatal(1, "vaddr text read");
  205. return *p;
  206. case Data:
  207. *p = emalloc(BY2PG);
  208. foff = s->fileoff+(off*BY2PG);
  209. if(seek(text, foff, 0) < 0)
  210. fatal(1, "vaddr text seek");
  211. n = read(text, *p, BY2PG);
  212. if(n < 0)
  213. fatal(1, "vaddr text read");
  214. if(foff + n > s->fileend) {
  215. l = BY2PG - (s->fileend-foff);
  216. a = *p+(s->fileend-foff);
  217. memset(a, 0, l);
  218. }
  219. return *p;
  220. case Bss:
  221. case Stack:
  222. *p = emalloc(BY2PG);
  223. return *p;
  224. }
  225. }
  226. }
  227. Bprint(bioout, "data_access_MMU_miss [addr 0x%.8lux]\n", addr);
  228. longjmp(errjmp, 0);
  229. return 0; /*to stop compiler whining*/
  230. }