| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Jakub Jermar
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup sparc64mm
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <arch/mm/as.h>
|
|---|
| 36 | #include <arch/mm/tlb.h>
|
|---|
| 37 | #include <genarch/mm/page_ht.h>
|
|---|
| 38 | #include <genarch/mm/asid_fifo.h>
|
|---|
| 39 | #include <debug.h>
|
|---|
| 40 | #include <config.h>
|
|---|
| 41 |
|
|---|
| 42 | #ifdef CONFIG_TSB
|
|---|
| 43 |
|
|---|
| 44 | #include <arch/mm/tsb.h>
|
|---|
| 45 | #include <arch/asm.h>
|
|---|
| 46 | #include <mm/frame.h>
|
|---|
| 47 | #include <bitops.h>
|
|---|
| 48 | #include <macros.h>
|
|---|
| 49 |
|
|---|
| 50 | #endif /* CONFIG_TSB */
|
|---|
| 51 |
|
|---|
| 52 | /** Architecture dependent address space init. */
|
|---|
| 53 | void as_arch_init(void)
|
|---|
| 54 | {
|
|---|
| 55 | if (config.cpu_active == 1) {
|
|---|
| 56 | as_operations = &as_ht_operations;
|
|---|
| 57 | asid_fifo_init();
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | int as_constructor_arch(as_t *as, unsigned int flags)
|
|---|
| 62 | {
|
|---|
| 63 | #ifdef CONFIG_TSB
|
|---|
| 64 | /*
|
|---|
| 65 | * The order must be calculated with respect to the emulated
|
|---|
| 66 | * 16K page size.
|
|---|
| 67 | *
|
|---|
| 68 | */
|
|---|
| 69 | uint8_t order = fnzb32(((ITSB_ENTRY_COUNT + DTSB_ENTRY_COUNT) *
|
|---|
| 70 | sizeof(tsb_entry_t)) >> FRAME_WIDTH);
|
|---|
| 71 |
|
|---|
| 72 | uintptr_t tsb = (uintptr_t) frame_alloc(order, flags | FRAME_KA);
|
|---|
| 73 |
|
|---|
| 74 | if (!tsb)
|
|---|
| 75 | return -1;
|
|---|
| 76 |
|
|---|
| 77 | as->arch.itsb = (tsb_entry_t *) tsb;
|
|---|
| 78 | as->arch.dtsb = (tsb_entry_t *) (tsb + ITSB_ENTRY_COUNT *
|
|---|
| 79 | sizeof(tsb_entry_t));
|
|---|
| 80 |
|
|---|
| 81 | memsetb(as->arch.itsb,
|
|---|
| 82 | (ITSB_ENTRY_COUNT + DTSB_ENTRY_COUNT) * sizeof(tsb_entry_t), 0);
|
|---|
| 83 | #endif
|
|---|
| 84 |
|
|---|
| 85 | return 0;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | int as_destructor_arch(as_t *as)
|
|---|
| 89 | {
|
|---|
| 90 | #ifdef CONFIG_TSB
|
|---|
| 91 | /*
|
|---|
| 92 | * The count must be calculated with respect to the emualted 16K page
|
|---|
| 93 | * size.
|
|---|
| 94 | */
|
|---|
| 95 | size_t cnt = ((ITSB_ENTRY_COUNT + DTSB_ENTRY_COUNT) *
|
|---|
| 96 | sizeof(tsb_entry_t)) >> FRAME_WIDTH;
|
|---|
| 97 | frame_free(KA2PA((uintptr_t) as->arch.itsb));
|
|---|
| 98 |
|
|---|
| 99 | return cnt;
|
|---|
| 100 | #else
|
|---|
| 101 | return 0;
|
|---|
| 102 | #endif
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | int as_create_arch(as_t *as, unsigned int flags)
|
|---|
| 106 | {
|
|---|
| 107 | #ifdef CONFIG_TSB
|
|---|
| 108 | tsb_invalidate(as, 0, (size_t) -1);
|
|---|
| 109 | #endif
|
|---|
| 110 |
|
|---|
| 111 | return 0;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /** Perform sparc64-specific tasks when an address space becomes active on the
|
|---|
| 115 | * processor.
|
|---|
| 116 | *
|
|---|
| 117 | * Install ASID and map TSBs.
|
|---|
| 118 | *
|
|---|
| 119 | * @param as Address space.
|
|---|
| 120 | */
|
|---|
| 121 | void as_install_arch(as_t *as)
|
|---|
| 122 | {
|
|---|
| 123 | tlb_context_reg_t ctx;
|
|---|
| 124 |
|
|---|
| 125 | /*
|
|---|
| 126 | * Note that we don't and may not lock the address space. That's ok
|
|---|
| 127 | * since we only read members that are currently read-only.
|
|---|
| 128 | *
|
|---|
| 129 | * Moreover, the as->asid is protected by asidlock, which is being held.
|
|---|
| 130 | *
|
|---|
| 131 | */
|
|---|
| 132 |
|
|---|
| 133 | /*
|
|---|
| 134 | * Write ASID to secondary context register. The primary context
|
|---|
| 135 | * register has to be set from TL>0 so it will be filled from the
|
|---|
| 136 | * secondary context register from the TL=1 code just before switch to
|
|---|
| 137 | * userspace.
|
|---|
| 138 | *
|
|---|
| 139 | */
|
|---|
| 140 | ctx.v = 0;
|
|---|
| 141 | ctx.context = as->asid;
|
|---|
| 142 | mmu_secondary_context_write(ctx.v);
|
|---|
| 143 |
|
|---|
| 144 | #ifdef CONFIG_TSB
|
|---|
| 145 | uintptr_t base = ALIGN_DOWN(config.base, 1 << KERNEL_PAGE_WIDTH);
|
|---|
| 146 |
|
|---|
| 147 | ASSERT(as->arch.itsb && as->arch.dtsb);
|
|---|
| 148 |
|
|---|
| 149 | uintptr_t tsb = (uintptr_t) as->arch.itsb;
|
|---|
| 150 |
|
|---|
| 151 | if (!overlaps(tsb, 8 * MMU_PAGE_SIZE, base, 1 << KERNEL_PAGE_WIDTH)) {
|
|---|
| 152 | /*
|
|---|
| 153 | * TSBs were allocated from memory not covered
|
|---|
| 154 | * by the locked 4M kernel DTLB entry. We need
|
|---|
| 155 | * to map both TSBs explicitly.
|
|---|
| 156 | *
|
|---|
| 157 | */
|
|---|
| 158 | dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_NUCLEUS, tsb);
|
|---|
| 159 | dtlb_insert_mapping(tsb, KA2PA(tsb), PAGESIZE_64K, true, true);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /*
|
|---|
| 163 | * Setup TSB Base registers.
|
|---|
| 164 | *
|
|---|
| 165 | */
|
|---|
| 166 | tsb_base_reg_t tsb_base;
|
|---|
| 167 |
|
|---|
| 168 | tsb_base.value = 0;
|
|---|
| 169 | tsb_base.size = TSB_SIZE;
|
|---|
| 170 | tsb_base.split = 0;
|
|---|
| 171 |
|
|---|
| 172 | tsb_base.base = ((uintptr_t) as->arch.itsb) >> MMU_PAGE_WIDTH;
|
|---|
| 173 | itsb_base_write(tsb_base.value);
|
|---|
| 174 | tsb_base.base = ((uintptr_t) as->arch.dtsb) >> MMU_PAGE_WIDTH;
|
|---|
| 175 | dtsb_base_write(tsb_base.value);
|
|---|
| 176 |
|
|---|
| 177 | #if defined (US3)
|
|---|
| 178 | /*
|
|---|
| 179 | * Clear the extension registers.
|
|---|
| 180 | * In HelenOS, primary and secondary context registers contain
|
|---|
| 181 | * equal values and kernel misses (context 0, ie. the nucleus context)
|
|---|
| 182 | * are excluded from the TSB miss handler, so it makes no sense
|
|---|
| 183 | * to have separate TSBs for primary, secondary and nucleus contexts.
|
|---|
| 184 | * Clearing the extension registers will ensure that the value of the
|
|---|
| 185 | * TSB Base register will be used as an address of TSB, making the code
|
|---|
| 186 | * compatible with the US port.
|
|---|
| 187 | *
|
|---|
| 188 | */
|
|---|
| 189 | itsb_primary_extension_write(0);
|
|---|
| 190 | itsb_nucleus_extension_write(0);
|
|---|
| 191 | dtsb_primary_extension_write(0);
|
|---|
| 192 | dtsb_secondary_extension_write(0);
|
|---|
| 193 | dtsb_nucleus_extension_write(0);
|
|---|
| 194 | #endif
|
|---|
| 195 | #endif
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /** Perform sparc64-specific tasks when an address space is removed from the
|
|---|
| 199 | * processor.
|
|---|
| 200 | *
|
|---|
| 201 | * Demap TSBs.
|
|---|
| 202 | *
|
|---|
| 203 | * @param as Address space.
|
|---|
| 204 | */
|
|---|
| 205 | void as_deinstall_arch(as_t *as)
|
|---|
| 206 | {
|
|---|
| 207 | /*
|
|---|
| 208 | * Note that we don't and may not lock the address space. That's ok
|
|---|
| 209 | * since we only read members that are currently read-only.
|
|---|
| 210 | *
|
|---|
| 211 | * Moreover, the as->asid is protected by asidlock, which is being held.
|
|---|
| 212 | *
|
|---|
| 213 | */
|
|---|
| 214 |
|
|---|
| 215 | #ifdef CONFIG_TSB
|
|---|
| 216 | uintptr_t base = ALIGN_DOWN(config.base, 1 << KERNEL_PAGE_WIDTH);
|
|---|
| 217 |
|
|---|
| 218 | ASSERT(as->arch.itsb && as->arch.dtsb);
|
|---|
| 219 |
|
|---|
| 220 | uintptr_t tsb = (uintptr_t) as->arch.itsb;
|
|---|
| 221 |
|
|---|
| 222 | if (!overlaps(tsb, 8 * MMU_PAGE_SIZE, base, 1 << KERNEL_PAGE_WIDTH)) {
|
|---|
| 223 | /*
|
|---|
| 224 | * TSBs were allocated from memory not covered
|
|---|
| 225 | * by the locked 4M kernel DTLB entry. We need
|
|---|
| 226 | * to demap the entry installed by as_install_arch().
|
|---|
| 227 | */
|
|---|
| 228 | dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_NUCLEUS, tsb);
|
|---|
| 229 | }
|
|---|
| 230 | #endif
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | /** @}
|
|---|
| 234 | */
|
|---|