| 1 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // File : vm_handler.c |
|---|
| 3 | // Date : 01/07/2012 |
|---|
| 4 | // Author : alain greiner |
|---|
| 5 | // Copyright (c) UPMC-LIP6 |
|---|
| 6 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 7 | // The vm_handler.c and vm_handler.h files are part ot the GIET nano kernel. |
|---|
| 8 | // They contains the kernel data structures and functions used to dynamically |
|---|
| 9 | // handle the iommu page table. |
|---|
| 10 | // |
|---|
| 11 | // TODO : We must transfer here the functions used to statically build |
|---|
| 12 | // the page tables associated to the various vspaces (now in boot_handler.c) |
|---|
| 13 | // |
|---|
| 14 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 15 | |
|---|
| 16 | #include <vm_handler.h> |
|---|
| 17 | #include <sys_handler.h> |
|---|
| 18 | #include <common.h> |
|---|
| 19 | #include <giet_config.h> |
|---|
| 20 | |
|---|
| 21 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 22 | // Global variable : IOMMU page table |
|---|
| 23 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 24 | |
|---|
| 25 | __attribute__((section (".iommu"))) page_table_t _iommu_ptab; |
|---|
| 26 | |
|---|
| 27 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 28 | // _iommu_add_pte2() |
|---|
| 29 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 30 | void _iommu_add_pte2( |
|---|
| 31 | unsigned int ix1, |
|---|
| 32 | unsigned int ix2, |
|---|
| 33 | unsigned int ppn, |
|---|
| 34 | unsigned int flags) { |
|---|
| 35 | unsigned int ptba; |
|---|
| 36 | unsigned int * pt_ppn; |
|---|
| 37 | unsigned int * pt_flags; |
|---|
| 38 | |
|---|
| 39 | // get pointer on iommu page table |
|---|
| 40 | page_table_t * pt = &_iommu_ptab; |
|---|
| 41 | |
|---|
| 42 | // get ptba and update PT2 |
|---|
| 43 | if ((pt->pt1[ix1] & PTE_V) == 0) { |
|---|
| 44 | _puts("\n[GIET ERROR] in iommu_add_pte2 function\n"); |
|---|
| 45 | _puts("the IOMMU PT1 entry is not mapped / ix1 = "); |
|---|
| 46 | _putx( ix1 ); |
|---|
| 47 | _puts("\n"); |
|---|
| 48 | _exit(); |
|---|
| 49 | } |
|---|
| 50 | else { |
|---|
| 51 | ptba = pt->pt1[ix1] << 12; |
|---|
| 52 | pt_flags = (unsigned int *) (ptba + 8 * ix2); |
|---|
| 53 | pt_ppn = (unsigned int *) (ptba + 8 * ix2 + 4); |
|---|
| 54 | *pt_flags = flags; |
|---|
| 55 | *pt_ppn = ppn; |
|---|
| 56 | } |
|---|
| 57 | } // end _iommu_add_pte2() |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 61 | // _iommu_inval_pte2() |
|---|
| 62 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 63 | void _iommu_inval_pte2(unsigned int ix1, unsigned int ix2) { |
|---|
| 64 | unsigned int ptba; |
|---|
| 65 | unsigned int * pt_flags; |
|---|
| 66 | |
|---|
| 67 | // get pointer on iommu page table |
|---|
| 68 | page_table_t * pt = &_iommu_ptab; |
|---|
| 69 | |
|---|
| 70 | // get ptba and inval PTE2 |
|---|
| 71 | if ((pt->pt1[ix1] & PTE_V) == 0) { |
|---|
| 72 | _puts("\n[GIET ERROR] in iommu_inval_pte2 function\n"); |
|---|
| 73 | _puts("the IOMMU PT1 entry is not mapped / ix1 = "); |
|---|
| 74 | _putx( ix1 ); |
|---|
| 75 | _puts("\n"); |
|---|
| 76 | _exit(); |
|---|
| 77 | } |
|---|
| 78 | else { |
|---|
| 79 | ptba = pt->pt1[ix1] << 12; |
|---|
| 80 | pt_flags = (unsigned int *) (ptba + 8 * ix2); |
|---|
| 81 | *pt_flags = 0; |
|---|
| 82 | } |
|---|
| 83 | } // end _iommu_inval_pte2() |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 87 | // _v2p_translate() |
|---|
| 88 | // Returns 0 if success, 1 if PTE1 or PTE2 unmapped |
|---|
| 89 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 90 | unsigned int _v2p_translate( |
|---|
| 91 | page_table_t * pt, |
|---|
| 92 | unsigned int vpn, |
|---|
| 93 | unsigned int * ppn, |
|---|
| 94 | unsigned int * flags) { |
|---|
| 95 | unsigned int ptba; |
|---|
| 96 | register unsigned int * pte2; |
|---|
| 97 | register unsigned int flags_value; |
|---|
| 98 | register unsigned int ppn_value; |
|---|
| 99 | |
|---|
| 100 | unsigned int ix1 = vpn >> 9; |
|---|
| 101 | unsigned int ix2 = vpn & 0x1FF; |
|---|
| 102 | /* |
|---|
| 103 | _puts("\n\n********************** entering v2p_translate"); |
|---|
| 104 | _puts("\n - pt = "); |
|---|
| 105 | _putx( (unsigned int)pt ); |
|---|
| 106 | _puts("\n - vpn = "); |
|---|
| 107 | _putx( vpn << 12 ); |
|---|
| 108 | _puts("\n - ptba = "); |
|---|
| 109 | _putx( pt->pt1[ix1] << 12 ) ; |
|---|
| 110 | _puts("\n - &pte2 = "); |
|---|
| 111 | _putx( (pt->pt1[ix1] << 12) + 8*ix2 ); |
|---|
| 112 | _puts("\n - flags = "); |
|---|
| 113 | _putx( *(unsigned int*)((pt->pt1[ix1] << 12) + 8*ix2) ); |
|---|
| 114 | _puts("\n"); |
|---|
| 115 | */ |
|---|
| 116 | // check PTE1 mapping |
|---|
| 117 | if ((pt->pt1[ix1] & PTE_V) == 0) { |
|---|
| 118 | return 1; |
|---|
| 119 | } |
|---|
| 120 | else { |
|---|
| 121 | // get physical addresses of pte2 |
|---|
| 122 | ptba = pt->pt1[ix1] << 12; |
|---|
| 123 | pte2 = (unsigned int *) (ptba + 8 * ix2); |
|---|
| 124 | |
|---|
| 125 | // gets ppn_value and flags_value, after temporary DTLB desactivation |
|---|
| 126 | asm volatile ( |
|---|
| 127 | "li $27, 0xFFFFFFFE \n" /* Mask for IE bits */ |
|---|
| 128 | "mfc0 $26, $12 \n" /* save SR */ |
|---|
| 129 | "and $27, $26, $27 \n" |
|---|
| 130 | "mtc0 $27, $12 \n" /* disable Interrupts */ |
|---|
| 131 | |
|---|
| 132 | "li $27, 0xB \n" |
|---|
| 133 | "mtc2 $27, $1 \n" /* DTLB unactivated */ |
|---|
| 134 | |
|---|
| 135 | "move $27, %2 \n" /* $27 <= pte2 */ |
|---|
| 136 | "lw %0, 0($27) \n" /* read flags */ |
|---|
| 137 | "lw %1, 4($27) \n" /* read ppn */ |
|---|
| 138 | |
|---|
| 139 | "li $27, 0xF \n" |
|---|
| 140 | "mtc2 $27, $1 \n" /* DTLB activated */ |
|---|
| 141 | |
|---|
| 142 | "mtc0 $26, $12 \n" /* restore SR */ |
|---|
| 143 | : "=r" (flags_value), "=r" (ppn_value) |
|---|
| 144 | : "r" (pte2) |
|---|
| 145 | : "$26","$27","$8"); |
|---|
| 146 | |
|---|
| 147 | // check PTE2 mapping |
|---|
| 148 | if ((flags_value & PTE_V) == 0) { |
|---|
| 149 | return 1; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | // set return values |
|---|
| 153 | *ppn = ppn_value; |
|---|
| 154 | *flags = flags_value; |
|---|
| 155 | } |
|---|
| 156 | return 0; |
|---|
| 157 | } // end _v2p_translate() |
|---|
| 158 | |
|---|
| 159 | // Local Variables: |
|---|
| 160 | // tab-width: 4 |
|---|
| 161 | // c-basic-offset: 4 |
|---|
| 162 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
|---|
| 163 | // indent-tabs-mode: nil |
|---|
| 164 | // End: |
|---|
| 165 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
|---|
| 166 | |
|---|
| 167 | |
|---|