|
1 |
| -#include <hal/mmu.h> |
2 |
| - |
3 |
| -#define PAGE_SIZE (4096) |
4 |
| - |
5 |
| -#define MAX_MMU_SLOTS 10 |
6 |
| -#define NO_EMPTY_SLOT (MAX_MMU_SLOTS + 1) |
7 |
| - |
8 |
| -#define A_BAD_ADDR (0) |
9 |
| - |
10 |
| -#define NULL (0) |
11 |
| - |
12 |
| -#define get_pfn(x) (x & ~(PAGE_SIZE - 1)) |
13 |
| - |
14 |
| -struct mmu_mapping { |
15 |
| - |
16 |
| - unsigned int vaddr; |
17 |
| - unsigned int paddr; |
18 |
| - char valid; |
19 |
| - |
20 |
| -} mappings[10]; |
21 |
| - |
22 | 1 | /*
|
23 |
| - * This records in a global structure all MMU mappings |
24 |
| - * If such a mapping already exists the function returns immediately. |
25 |
| - * If such a mapping does not exist yet, vaddr is mapped to paddr and |
26 |
| - * the mapping is recorded in the mappings[] global structure array in |
27 |
| - * an empty slot. |
28 |
| - * If there is no empty slot anymore then we fail |
| 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/>. |
29 | 16 | */
|
30 | 17 |
|
31 |
| -unsigned int mmu_map(unsigned int vaddr, unsigned int paddr) { |
32 |
| - int i; |
33 |
| - int empty_slot = NO_EMPTY_SLOT; |
34 |
| - vaddr = get_pfn(vaddr); |
35 |
| - |
36 |
| - for (i = 0 ; i < MAX_MMU_SLOTS ; ++i) |
37 |
| - { |
38 |
| - if (!mappings[i].valid) |
39 |
| - empty_slot = i; |
40 |
| - if (vaddr == mappings[i].vaddr && paddr == mappings[i].paddr) |
41 |
| - return 1; |
42 |
| - } |
43 |
| - |
44 |
| - if (empty_slot == NO_EMPTY_SLOT) |
45 |
| - return empty_slot; |
46 |
| - |
47 |
| - mappings[empty_slot].vaddr = vaddr; |
48 |
| - mappings[empty_slot].paddr = paddr; |
49 |
| - mappings[empty_slot].valid = 1; |
50 |
| - mmu_dtlb_map(vaddr, paddr); |
51 |
| - |
52 |
| - return 1; |
53 |
| -} |
| 18 | +#include <hal/mmu.h> |
| 19 | +#include <base/mmu.h> |
| 20 | +#include <base/stdio.h> |
54 | 21 |
|
55 |
| -unsigned int get_mmu_mapping_for(unsigned int vaddr) { |
56 |
| - int i; |
57 |
| - vaddr = get_pfn(vaddr); |
| 22 | +void dtlb_exception_handling_tests() { |
58 | 23 |
|
59 |
| - for (i = 0 ; i < MAX_MMU_SLOTS ; ++i) |
60 |
| - if (mappings[i].valid && vaddr == mappings[i].vaddr) |
61 |
| - return mappings[i].paddr; |
| 24 | + register unsigned int stack, addr; |
| 25 | + unsigned int data; |
| 26 | + int ret; |
62 | 27 |
|
63 |
| - return A_BAD_ADDR; |
64 |
| -} |
| 28 | + asm volatile("mv %0, sp" : "=r"(stack) :: ); |
65 | 29 |
|
66 |
| -unsigned int invalidate_mmu_mapping(unsigned int vaddr) { |
67 |
| - int i; |
68 |
| - vaddr = get_pfn(vaddr); |
69 |
| - for (i = 0 ; i < MAX_MMU_SLOTS ; ++i) |
70 |
| - { |
71 |
| - if (mappings[i].valid && vaddr == mappings[i].vaddr) { |
72 |
| - mmu_dtlb_invalidate(vaddr); |
73 |
| - mappings[i].valid = 0; |
74 |
| - return 1; |
75 |
| - } |
76 |
| - } |
77 |
| - return 0; |
78 |
| -} |
| 30 | + ret = mmu_map(stack, stack); |
| 31 | + check_for_error(ret); |
79 | 32 |
|
80 |
| -static void panic(void) { |
81 |
| - puts("PANIC !"); |
82 |
| - while(1) |
83 |
| - asm volatile("nop"); |
84 |
| -} |
| 33 | + ret = mmu_map(stack-0x1000, stack-0x1000); |
| 34 | + check_for_error(ret); |
85 | 35 |
|
86 |
| -static void check_for_error(int ret) { |
87 |
| - if (ret) |
88 |
| - return; |
| 36 | + printf("stack == 0x%08X\n", stack); |
89 | 37 |
|
90 |
| - if (ret == NO_EMPTY_SLOT) { |
91 |
| - puts("No empty slot in MMU mappings structure anymore"); |
92 |
| - panic(); |
93 |
| - } |
| 38 | + addr = 0x44004004; |
94 | 39 |
|
95 |
| - if ( !ret ) { |
96 |
| - puts("Unknown issue"); |
97 |
| - panic(); |
98 |
| - } |
99 |
| -} |
| 40 | + printf("\n=> Mapping 0x%08X to 0x%08X\n", addr, addr); |
| 41 | + ret = mmu_map(addr, addr); |
| 42 | + check_for_error(ret); |
100 | 43 |
|
101 |
| -void dtlb_exception_handling_tests() { |
| 44 | + data = 42; |
| 45 | + printf("=> Writing %d to physical address 0x%08X\n", data, addr); |
| 46 | + *(unsigned int *)addr = data; |
102 | 47 |
|
103 |
| - register unsigned int stack, addr, data; |
104 |
| - int ret; |
| 48 | + printf("=> Activating the MMU and reading form virtual address 0x%08X\n", addr); |
| 49 | + data = read_word_with_mmu_enabled(addr); |
| 50 | + printf("\n<= Reading %d from virtual address 0x%08X\n\n", data, addr); |
105 | 51 |
|
106 |
| - asm volatile("mv %0, sp" : "=r"(stack) :: ); |
| 52 | + printf("=> Invalidating the mapping of virtual address 0x%08X in the TLB\n", addr); |
| 53 | + mmu_dtlb_invalidate(addr); |
107 | 54 |
|
108 |
| - ret = mmu_map(stack, stack); |
109 |
| - check_for_error(ret); |
| 55 | + data = 43; |
| 56 | + printf("=> Writing %d to physical address 0x%08X\n", data, addr); |
| 57 | + *(unsigned int *)addr = data; |
110 | 58 |
|
111 |
| - ret = mmu_map(stack-0x1000, stack-0x1000); |
| 59 | + printf("=> Activating the MMU and reading form virtual address 0x%08X\n", addr); |
| 60 | + data = read_word_with_mmu_enabled(addr); |
| 61 | + printf("\n<= Reading %d from virtual address 0x%08X\n\n", data, addr); |
| 62 | + |
| 63 | + printf("=> Mapping 0x%08X to 0%08X\n", addr, addr+0x1000); |
| 64 | + ret = mmu_map(addr, addr+0x1000); // Map to something else |
112 | 65 | check_for_error(ret);
|
113 | 66 |
|
114 |
| - printf("stack == 0x%08X\n", stack); |
| 67 | + printf("=> Invalidating the mapping of virtual address 0x%08X in the TLB\n", addr); |
| 68 | + mmu_dtlb_invalidate(addr); // AND invalidate the mapping |
| 69 | + |
| 70 | + data = 44; |
| 71 | + printf("=> Writting %d to physical address 0x%08X\n", data, addr+0x1000); |
| 72 | + *(unsigned int *)(addr + 0x1000) = data; |
115 | 73 |
|
116 |
| - addr = 0x44002342; // Random address |
117 |
| - *(unsigned int *)addr = 42; |
118 |
| -// mmu_map(addr, addr); |
119 |
| - |
120 |
| - printf("Address 0x%08X mapped to itself, value : ", addr); |
121 |
| - |
122 |
| - asm volatile( |
123 |
| - "xor r11, r11, r11\n\t" |
124 |
| - "ori r11, r11, 0x11\n\t" |
125 |
| - "wcsr tlbctrl, r11\n\t" // this activates the mmu |
126 |
| - "xor r0, r0, r0\n\t" |
127 |
| - "xor r11, r11, r11\n\t" |
128 |
| - "or r11, r11, %1\n\t" |
129 |
| - "lw %0, (r11+0)\n\t" |
130 |
| - "xor r11, r11, r11\n\t" |
131 |
| - "ori r11, r11, 0x9\n\t" |
132 |
| - "wcsr tlbctrl, r11\n\t" // this disactivates the mmu |
133 |
| - "xor r0, r0, r0" : "=&r"(data) : "r"(addr) : "r11" |
134 |
| - ); |
135 |
| - |
136 |
| - printf("%d\n", data); |
137 |
| - |
138 |
| - invalidate_mmu_mapping(addr); |
139 |
| - |
140 |
| - printf("DTLB has just been invalidated, next access to 0x%08X should trigger a DTLB exception\n", addr); |
141 |
| - |
142 |
| - printf("Address 0x%08X not mapped, value : ", addr); |
143 |
| - |
144 |
| - asm volatile( |
145 |
| - "xor r11, r11, r11\n\t" |
146 |
| - "ori r11, r11, 0x11\n\t" |
147 |
| - "wcsr tlbctrl, r11\n\t" // this activates the mmu |
148 |
| - "xor r0, r0, r0\n\t" |
149 |
| - "xor r11, r11, r11\n\t" |
150 |
| - "or r11, r11, %1\n\t" |
151 |
| - "lw %0, (r11+0)\n\t" |
152 |
| - "xor r11, r11, r11\n\t" |
153 |
| - "ori r11, r11, 0x9\n\t" |
154 |
| - "wcsr tlbctrl, r11\n\t" // this disactivates the mmu |
155 |
| - "xor r0, r0, r0" : "=&r"(data) : "r"(addr) : "r11" |
156 |
| - ); |
157 |
| - |
158 |
| - printf("%d\n", data); |
| 74 | + printf("=> Activating the MMU and reading form virtual address 0x%08X\n", addr); |
| 75 | + data = read_word_with_mmu_enabled(addr); |
| 76 | + printf("\n<= Reading %d from virtual address 0x%08X\n\n", data, addr); |
159 | 77 |
|
160 | 78 | }
|
0 commit comments