|
| 1 | +/* |
| 2 | + * Milkymist SoC (Software) |
| 3 | + * Copyright (C) 2012 Yann Sionneau <yann.sionneau@gmail.com> |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, version 3 of the License. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <hal/mmu.h> |
| 19 | +#include <base/mmu.h> |
| 20 | +#include <base/stdio.h> |
| 21 | + |
| 22 | +#ifdef __ASSEMBLER__ |
| 23 | +#define MMPTR(x) x |
| 24 | +#else |
| 25 | +#define MMPTR(x) (*((volatile unsigned int *)(x))) |
| 26 | +#endif |
| 27 | +#define CSR_UART_RXTX MMPTR(0xe0000000) |
| 28 | +#define UART_STAT_THRE (0x1) |
| 29 | +#define CSR_UART_STAT MMPTR(0xe0000008) |
| 30 | + |
| 31 | +#define OPCODE_MASK (0xfc000000) |
| 32 | +#define OPCODE_BI (0xe0000000) |
| 33 | +#define OPCODE_CALLI (0xf8000000) |
| 34 | + |
| 35 | +unsigned int relocate(unsigned int *opcode, unsigned int *reloc_addr) |
| 36 | +{ |
| 37 | + unsigned int reloc_diff = reloc_addr - opcode; |
| 38 | + unsigned int nopcode; |
| 39 | + int imm; |
| 40 | + |
| 41 | + switch (*opcode & OPCODE_MASK) |
| 42 | + { |
| 43 | + case OPCODE_BI: |
| 44 | + puts("This is a BI"); |
| 45 | + return *opcode; |
| 46 | + break; |
| 47 | + |
| 48 | + case OPCODE_CALLI: |
| 49 | + puts("This is a CALLI"); |
| 50 | + printf("reloc_diff == 0x%08X , reloc_addr = 0x%08X\n", reloc_diff, (unsigned int)reloc_addr); |
| 51 | + imm = (*opcode) & ~(OPCODE_MASK); |
| 52 | + nopcode = (~OPCODE_MASK) & (imm - reloc_diff); |
| 53 | + nopcode |= OPCODE_CALLI; |
| 54 | + return nopcode; |
| 55 | + break; |
| 56 | + |
| 57 | + default: |
| 58 | + return *opcode; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +void f(void) { |
| 63 | + unsigned int ret; |
| 64 | + char c = '@'; |
| 65 | + CSR_UART_RXTX = c; |
| 66 | + while(!(CSR_UART_STAT & UART_STAT_THRE)); |
| 67 | + puts("totolol"); |
| 68 | + printf("\n\ntest %d %c %s\n\n", 5, (char)186, "abcd"); |
| 69 | + puts("@"); |
| 70 | +// asm volatile("bi f" ::: ); // We intinitely loop to f() |
| 71 | + asm volatile("lw %0, (sp+4)" : "=r"(ret) :: ); |
| 72 | + printf("Return address == 0x%08X\n", ret); |
| 73 | + asm volatile("xor r0, r0, r0\n\t" |
| 74 | + "xor r0, r0, r0" ::: ); |
| 75 | +} |
| 76 | + |
| 77 | +void itlbtest(void) { |
| 78 | + register unsigned int stack, f_addr; |
| 79 | + unsigned int *p; |
| 80 | + unsigned int *pdest; |
| 81 | + unsigned int addr; |
| 82 | + int size_of_f = 10; |
| 83 | + |
| 84 | + asm volatile("mv %0, sp" : "=r"(stack) :: ); |
| 85 | + printf("stack == 0x%08X\n", stack); |
| 86 | + |
| 87 | + printf("f() is located at 0x%p\n", f); |
| 88 | + |
| 89 | + f_addr = 0x44004000; |
| 90 | + printf("Mapping f() into virtual memory at 0x%08X [physical == 0x%08X]\n", f_addr, f_addr+0x1000); |
| 91 | + |
| 92 | + for (addr = 0x00860000 ; addr <= 0x00875000 ; addr += 0x1000) |
| 93 | + mmu_map(addr, addr, ITLB_MAPPING | DTLB_MAPPING | MAPPING_CAN_READ); |
| 94 | + |
| 95 | + mmu_map(stack, stack, DTLB_MAPPING | MAPPING_CAN_READ | MAPPING_CAN_WRITE); |
| 96 | +// mmu_map(f_addr, f_addr + 0x1000, ITLB_MAPPING | MAPPING_CAN_READ); |
| 97 | + mmu_map(f, f, ITLB_MAPPING | MAPPING_CAN_READ); |
| 98 | + mmu_map(itlbtest, itlbtest, ITLB_MAPPING | MAPPING_CAN_READ); |
| 99 | +// mmu_map(call_function_with_itlb_enabled, call_function_with_itlb_enabled, ITLB_MAPPING | MAPPING_CAN_READ); |
| 100 | + puts("Mapping DONE"); |
| 101 | + |
| 102 | + puts("Printing MMU mappings : \n\n"); |
| 103 | + |
| 104 | + mmu_map_print(); |
| 105 | + |
| 106 | +// mmu_itlb_invalidate_line(f_addr); |
| 107 | + |
| 108 | + // We copy f's code to 0x44005000 |
| 109 | +// for (p = (unsigned int *)f, pdest = (unsigned int *)0x44005000 ; p < (unsigned int *)f + size_of_f ; p++, pdest++) |
| 110 | +// { |
| 111 | +// unsigned int *vpdest = (unsigned int *)((unsigned int)pdest - 0x1000); // Virtual destination is physical one - 0x1000 |
| 112 | +// printf("Before : 0x%08X\n", *p); |
| 113 | +// *pdest = relocate(p, vpdest); |
| 114 | +// printf("After : 0x%08X\n", *pdest); |
| 115 | +// *pdest = *p; |
| 116 | +// } |
| 117 | +// puts("Copy DONE"); |
| 118 | + |
| 119 | + asm volatile("wcsr ICC, r0\n\t" |
| 120 | + "xor r0, r0, r0\n\t" |
| 121 | + "xor r0, r0, r0\n\t" |
| 122 | + "xor r0, r0, r0\n\t" |
| 123 | + "xor r0, r0, r0"); |
| 124 | + asm volatile("wcsr DCC, r0\n\t" |
| 125 | + "xor r0, r0, r0"); |
| 126 | + puts("Instruction and Data caches have been invalidated"); |
| 127 | + |
| 128 | + call_function_with_itlb_enabled(f); |
| 129 | + disable_itlb(); |
| 130 | + disable_dtlb(); |
| 131 | + puts("Call DONE"); |
| 132 | +} |
0 commit comments