Advertisement
Guest User

Milkymist MMU ITLB test

a guest
Jun 29th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.76 KB | None | 0 0
  1. #define OPCODE_MASK     (0xfc000000)
  2. #define OPCODE_BI   (0xe0000000)
  3. #define OPCODE_CALLI    (0xf8000000)
  4.  
  5. unsigned int relocate(unsigned int *opcode, unsigned int *reloc_addr)
  6. {
  7.     unsigned int reloc_diff = reloc_addr - opcode;
  8.     unsigned int nopcode;
  9.     int imm;
  10.  
  11.     switch (*opcode & OPCODE_MASK)
  12.     {
  13.         case OPCODE_BI:
  14.             puts("This is a BI");
  15.             return *opcode;
  16.         break;
  17.  
  18.         case OPCODE_CALLI:
  19.             puts("This is a CALLI");
  20. //          printf("reloc_diff == 0x%08X , reloc_addr = 0x%08X\n", reloc_diff, (unsigned int)reloc_addr);
  21.             imm = (*opcode) & ~(OPCODE_MASK);
  22.             nopcode = (~OPCODE_MASK) & (imm - reloc_diff);
  23.             nopcode |= OPCODE_CALLI;
  24.             return nopcode;
  25.         break;
  26.  
  27.         default:
  28.         return *opcode;
  29.     }
  30. }
  31.  
  32. void f(void) {
  33. //  char c = '@';
  34. //  CSR_UART_RXTX = c;
  35.     puts("@");
  36. //  asm volatile("bi f" ::: ); // We intinitely loop to f()
  37.     asm volatile("xor r0, r0, r0\n\t"
  38.              "xor r0, r0, r0" ::: );
  39. }
  40.  
  41. void itlbtest(void) {
  42.     register unsigned int stack, f_addr;
  43.     unsigned int *p;
  44.     unsigned int *pdest;
  45.     int size_of_f = 38;
  46.  
  47.     asm volatile("mv %0, sp" : "=r"(stack) :: );
  48.     printf("stack == 0x%08X\n", stack);
  49.  
  50.     printf("f() is located at 0x%p\n", f);
  51.  
  52.     f_addr = 0x44004000;
  53.     printf("Mapping f() into virtual memory at 0x%08X [physical == 0x%08X]\n", f_addr, f_addr+0x1000);
  54.  
  55.     mmu_map(0x44002000, 0x44002000, ITLB_MAPPING | MAPPING_CAN_READ);
  56.     mmu_map(0x44003000, 0x44003000, DTLB_MAPPING | ITLB_MAPPING | MAPPING_CAN_READ | MAPPING_CAN_WRITE);
  57.     mmu_map(0x44006000, 0x44006000, DTLB_MAPPING | MAPPING_CAN_READ | MAPPING_CAN_WRITE);
  58.     mmu_map(0x44007000, 0x44007000, DTLB_MAPPING | MAPPING_CAN_READ | MAPPING_CAN_WRITE);
  59.     mmu_map(stack, stack, DTLB_MAPPING | ITLB_MAPPING | MAPPING_CAN_READ | MAPPING_CAN_WRITE);
  60.     mmu_map(f_addr, f_addr + 0x1000, ITLB_MAPPING | MAPPING_CAN_READ);
  61.     mmu_map(itlbtest, itlbtest, DTLB_MAPPING | ITLB_MAPPING | MAPPING_CAN_READ);
  62. //  mmu_map(call_function_with_itlb_enabled, call_function_with_itlb_enabled, ITLB_MAPPING | MAPPING_CAN_READ);
  63.     puts("Mapping DONE");
  64.  
  65. //  mmu_itlb_invalidate(f_addr);
  66.  
  67.     // We copy f's code to 0x44005000
  68.     for (p = (unsigned int *)f, pdest = (unsigned int *)0x44005000 ; p < (unsigned int *)f + size_of_f ; p++, pdest++)
  69.     {
  70.         unsigned int *vpdest = (unsigned int *)((unsigned int)pdest - 0x1000); // Virtual destination is physical one - 0x1000
  71. //      printf("Before : 0x%08X\n", *p);
  72.         *pdest = relocate(p, vpdest);
  73. //      printf("After : 0x%08X\n", *pdest);
  74.     }
  75.     puts("Copy DONE");
  76.  
  77.     asm volatile("wcsr DCC, r0\n\t"
  78.              "xor r0, r0, r0\n\t"
  79.              "xor r0, r0, r0\n\t"
  80.              "xor r0, r0, r0\n\t"
  81.              "xor r0, r0, r0");
  82.     asm volatile("wcsr ICC, r0\n\t"
  83.              "xor r0, r0, r0");
  84.     puts("Instruction and Data caches have been invalidated");
  85.  
  86.     call_function_with_itlb_enabled(f_addr);
  87.     disable_dtlb();
  88.     disable_itlb();
  89.     puts("Call DONE");
  90.     while(1)
  91.         asm volatile("xor r0, r0, r0");
  92. }
  93.  
  94. int main(int argc, char **argv)
  95. {
  96.     asm volatile("wcsr IE, r0");
  97. //  dtlb_load_test();
  98. //  dtlb_exception_handling_tests();
  99.     itlbtest();
  100.  
  101.     while (1)
  102.     {
  103.         asm volatile("xor r0, r0, r0");
  104.     }
  105.  
  106.     return 0;
  107. }
  108.  
  109.  
  110. f()'s code in assembly :
  111.  
  112. 44000218 <f>:
  113. 44000218:   37 9c ff f0     addi sp,sp,-16
  114. 4400021c:   5b 8b 00 10     sw (sp+16),r11
  115. 44000220:   5b 8c 00 0c     sw (sp+12),r12
  116. 44000224:   5b 8d 00 08     sw (sp+8),r13
  117. 44000228:   5b 9d 00 04     sw (sp+4),ra
  118. 4400022c:   f8 00 0a b6     calli 44002d04 <irq_getmask>
  119. 44000230:   b8 20 60 00     mv r12,r1
  120. 44000234:   34 01 00 01     mvi r1,1
  121. 44000238:   f8 00 0a b5     calli 44002d0c <irq_setmask>
  122. 4400023c:   f8 00 0a b2     calli 44002d04 <irq_getmask>
  123. 44000240:   b8 20 68 00     mv r13,r1
  124. 44000244:   78 01 44 00     mvhi r1,0x4400
  125. 44000248:   38 21 39 04     ori r1,r1,0x3904
  126. 4400024c:   28 2b 00 00     lw r11,(r1+0)
  127. 44000250:   34 01 00 00     mvi r1,0
  128. 44000254:   f8 00 0a ae     calli 44002d0c <irq_setmask>
  129. 44000258:   34 02 00 40     mvi r2,64
  130. 4400025c:   59 62 00 00     sw (r11+0),r2
  131. 44000260:   b9 a0 08 00     mv r1,r13
  132. 44000264:   f8 00 0a aa     calli 44002d0c <irq_setmask>
  133. 44000268:   f8 00 0a a7     calli 44002d04 <irq_getmask>
  134. 4400026c:   b8 20 68 00     mv r13,r1
  135. 44000270:   34 01 00 00     mvi r1,0
  136. 44000274:   f8 00 0a a6     calli 44002d0c <irq_setmask>
  137. 44000278:   34 02 00 0a     mvi r2,10
  138. 4400027c:   59 62 00 00     sw (r11+0),r2
  139. 44000280:   b9 a0 08 00     mv r1,r13
  140. 44000284:   f8 00 0a a2     calli 44002d0c <irq_setmask>
  141. 44000288:   b9 80 08 00     mv r1,r12
  142. 4400028c:   f8 00 0a a0     calli 44002d0c <irq_setmask>
  143. 44000290:   98 00 00 00     xor r0,r0,r0
  144. 44000294:   98 00 00 00     xor r0,r0,r0
  145. 44000298:   2b 9d 00 04     lw ra,(sp+4)
  146. 4400029c:   2b 8b 00 10     lw r11,(sp+16)
  147. 440002a0:   2b 8c 00 0c     lw r12,(sp+12)
  148. 440002a4:   2b 8d 00 08     lw r13,(sp+8)
  149. 440002a8:   37 9c 00 10     addi sp,sp,16
  150. 440002ac:   c3 a0 00 00     ret
  151.  
  152. f()'s code is copied to physical address 0x44005000
  153. Virtual address 0x44004000 is mapped to physical address 0x44005000 in ITLB
  154.  
  155. The code is "patched" on-the-fly to change CALLI relative jump offset taking into account new virtual location.
  156.  
  157. Then f() is called (using it's virtual address).
  158. f() contains 9 CALLI instructions, the puts() gets inlined.
  159.  
  160. The result of ISim simulation running the itlbtest :
  161.  
  162. ISim O.87xd (signature 0x8ddf5b5d)
  163. WARNING: A WEBPACK license was found.
  164. WARNING: Please use Xilinx License Configuration Manager to check out a full ISim license.
  165. WARNING: ISim will run in Lite mode. Please refer to the ISim documentation for more information on the differences between the Lite and the Full version.
  166. This is a Lite version of ISim.
  167. Time resolution is 1 ns
  168. ISim>
  169. # restart
  170. ISim>
  171. # run all
  172. Simulator is doing circuit initialization process.
  173. Finished circuit initialization process.
  174. stack == 0x440070B8
  175. f() is located at 0x44000218
  176. Mapping f() into virtual memory at 0x44004000 [physical == 0x44005000]
  177. stack == 0x440070A4
  178. mapping 0x44002000->0x44002000 in slot 9 [0x440040a4]
  179. stack == 0x440070A4
  180. mapping 0x44003000->0x44003000 in slot 8 [0x44004098]
  181. stack == 0x440070A4
  182. mapping 0x44006000->0x44006000 in slot 7 [0x4400408c]
  183. stack == 0x440070A4
  184. mapping 0x44007000->0x44007000 in slot 6 [0x44004080]
  185. stack == 0x440070A4
  186. Already mapped !
  187. stack == 0x440070A4
  188. mapping 0x44004000->0x44005000 in slot 5 [0x44004074]
  189. stack == 0x440070A4
  190. mapping 0x44000000->0x44000000 in slot 4 [0x44004068]
  191. Mapping DONE
  192. This is a CALLI
  193. This is a CALLI
  194. This is a CALLI
  195. This is a CALLI
  196. This is a CALLI
  197. This is a CALLI
  198. This is a CALLI
  199. This is a CALLI
  200. This is a CALLI
  201. Copy DONE
  202. Instruction and Data caches have been invalidated
  203. WARNING : ITLB MISS on addr 0x44000950 at time               822300
  204. WARNING : ITLB MISS on addr 0x44004000 at time               832830
  205. WARNING : DTLB MISS on addr 0x440070b8 at time               837770
  206. WARNING : ITLB MISS on addr 0x44002d04 at time               847210
  207. WARNING : DTLB MISS on addr 0x44003904 at time               855470
  208. @
  209. Call DONE
  210. Stopped at time : 1857240 ns : File "/home/yann/test_isim3/lm32_logic_op.v" Line 92
  211. ISim>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement