[4512d7e] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Jakub Jermar
|
---|
[4512d7e] | 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 |
|
---|
[f47fd19] | 29 | /** @addtogroup genarchmm
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[0f27b4c] | 33 | /**
|
---|
[b45c443] | 34 | * @file
|
---|
[76fca31] | 35 | * @brief ASID management.
|
---|
[4512d7e] | 36 | *
|
---|
| 37 | * Modern processor architectures optimize TLB utilization
|
---|
| 38 | * by using ASIDs (a.k.a. memory contexts on sparc64 and
|
---|
| 39 | * region identifiers on ia64). These ASIDs help to associate
|
---|
| 40 | * each TLB item with an address space, thus making
|
---|
| 41 | * finer-grained TLB invalidation possible.
|
---|
| 42 | *
|
---|
| 43 | * Unfortunatelly, there are usually less ASIDs available than
|
---|
| 44 | * there can be unique as_t structures (i.e. address spaces
|
---|
| 45 | * recognized by the kernel).
|
---|
| 46 | *
|
---|
| 47 | * When system runs short of ASIDs, it will attempt to steal
|
---|
| 48 | * ASID from an address space that has not been active for
|
---|
| 49 | * a while.
|
---|
| 50 | *
|
---|
[7e4e532] | 51 | * This code depends on the fact that ASIDS_ALLOCABLE
|
---|
| 52 | * is greater than number of supported CPUs (i.e. the
|
---|
| 53 | * amount of concurently active address spaces).
|
---|
| 54 | *
|
---|
[4512d7e] | 55 | * Architectures that don't have hardware support for address
|
---|
| 56 | * spaces do not compile with this file.
|
---|
| 57 | */
|
---|
| 58 |
|
---|
| 59 | #include <mm/asid.h>
|
---|
| 60 | #include <mm/as.h>
|
---|
| 61 | #include <mm/tlb.h>
|
---|
| 62 | #include <arch/mm/asid.h>
|
---|
| 63 | #include <synch/spinlock.h>
|
---|
[1068f6a] | 64 | #include <synch/mutex.h>
|
---|
[5c9a08b] | 65 | #include <adt/list.h>
|
---|
[4512d7e] | 66 | #include <debug.h>
|
---|
| 67 |
|
---|
[98000fb] | 68 | static size_t asids_allocated = 0;
|
---|
[4512d7e] | 69 |
|
---|
| 70 | /** Allocate free address space identifier.
|
---|
| 71 | *
|
---|
| 72 | * @return New ASID.
|
---|
| 73 | */
|
---|
| 74 | asid_t asid_get(void)
|
---|
| 75 | {
|
---|
| 76 | asid_t asid;
|
---|
| 77 | link_t *tmp;
|
---|
| 78 | as_t *as;
|
---|
| 79 |
|
---|
[1d432f9] | 80 | ASSERT(interrupts_disabled());
|
---|
| 81 | ASSERT(spinlock_locked(&asidlock));
|
---|
| 82 |
|
---|
[4512d7e] | 83 | /*
|
---|
| 84 | * Check if there is an unallocated ASID.
|
---|
| 85 | */
|
---|
| 86 |
|
---|
[a60c748] | 87 | if (asids_allocated == ASIDS_ALLOCABLE) {
|
---|
[4512d7e] | 88 |
|
---|
| 89 | /*
|
---|
| 90 | * All ASIDs are already allocated.
|
---|
| 91 | * Resort to stealing.
|
---|
| 92 | */
|
---|
| 93 |
|
---|
| 94 | /*
|
---|
| 95 | * Remove the first item on the list.
|
---|
| 96 | * It is guaranteed to belong to an
|
---|
| 97 | * inactive address space.
|
---|
| 98 | */
|
---|
[7e4e532] | 99 | ASSERT(!list_empty(&inactive_as_with_asid_head));
|
---|
| 100 | tmp = inactive_as_with_asid_head.next;
|
---|
[4512d7e] | 101 | list_remove(tmp);
|
---|
| 102 |
|
---|
[7e4e532] | 103 | as = list_get_instance(tmp, as_t, inactive_as_with_asid_link);
|
---|
[4512d7e] | 104 |
|
---|
| 105 | /*
|
---|
| 106 | * Steal the ASID.
|
---|
| 107 | * Note that the stolen ASID is not active.
|
---|
| 108 | */
|
---|
| 109 | asid = as->asid;
|
---|
| 110 | ASSERT(asid != ASID_INVALID);
|
---|
| 111 |
|
---|
[fa7d9c4] | 112 | /*
|
---|
| 113 | * Notify the address space from wich the ASID
|
---|
| 114 | * was stolen by invalidating its asid member.
|
---|
| 115 | */
|
---|
| 116 | as->asid = ASID_INVALID;
|
---|
[f1d1f5d3] | 117 |
|
---|
| 118 | /*
|
---|
| 119 | * If the architecture uses some software cache
|
---|
| 120 | * of TLB entries (e.g. TSB on sparc64), the
|
---|
| 121 | * cache must be invalidated as well.
|
---|
| 122 | */
|
---|
[98000fb] | 123 | as_invalidate_translation_cache(as, 0, (size_t) -1);
|
---|
[f1d1f5d3] | 124 |
|
---|
[4512d7e] | 125 | /*
|
---|
| 126 | * Get the system rid of the stolen ASID.
|
---|
| 127 | */
|
---|
[402eda5] | 128 | ipl_t ipl = tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0);
|
---|
[36b01bb2] | 129 | tlb_invalidate_asid(asid);
|
---|
[402eda5] | 130 | tlb_shootdown_finalize(ipl);
|
---|
[4512d7e] | 131 | } else {
|
---|
| 132 |
|
---|
| 133 | /*
|
---|
| 134 | * There is at least one unallocated ASID.
|
---|
| 135 | * Find it and assign it.
|
---|
| 136 | */
|
---|
| 137 |
|
---|
| 138 | asid = asid_find_free();
|
---|
| 139 | asids_allocated++;
|
---|
[88169d9] | 140 |
|
---|
| 141 | /*
|
---|
[f1d1f5d3] | 142 | * Purge the allocated ASID from TLBs.
|
---|
[88169d9] | 143 | */
|
---|
[402eda5] | 144 | ipl_t ipl = tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0);
|
---|
[88169d9] | 145 | tlb_invalidate_asid(asid);
|
---|
[402eda5] | 146 | tlb_shootdown_finalize(ipl);
|
---|
[4512d7e] | 147 | }
|
---|
| 148 |
|
---|
| 149 | return asid;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | /** Release address space identifier.
|
---|
| 153 | *
|
---|
| 154 | * This code relies on architecture
|
---|
| 155 | * dependent functionality.
|
---|
| 156 | *
|
---|
| 157 | * @param asid ASID to be released.
|
---|
| 158 | */
|
---|
| 159 | void asid_put(asid_t asid)
|
---|
| 160 | {
|
---|
| 161 | asids_allocated--;
|
---|
| 162 | asid_put_arch(asid);
|
---|
| 163 | }
|
---|
[b45c443] | 164 |
|
---|
[f47fd19] | 165 | /** @}
|
---|
[b45c443] | 166 | */
|
---|