The Pedigree Project  0.1
AsidManager.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "AsidManager.h"
21 #include "pedigree/kernel/Log.h"
22 #include "pedigree/kernel/panic.h"
23 
25 
27 {
28  return m_Instance;
29 }
30 
32 {
33 }
34 
36 {
37 }
38 
40 {
41  // Our algorithm is a linear search of multiple levels.
42  // Firstly we search for an ASID with a contention of zero.
43  // if that's not found, we search for one with <= 1,
44  // etc. The reason for the less than is that an asid could be given back
45  // while we are searching, and this covers all eventualities.
46 
47  for (int i = 0; i < 10; i++)
48  {
49  for (int j = 0; j < NUM_ASID; j++)
50  {
51  if (m_Asids[j] <= i)
52  {
53  // Lock it.
54  m_Mutex.acquire();
55  // Is it still valid?
56  if (m_Asids[j] <= 1)
57  {
58  // Success! If this ASID is contended, clear the TLB.
59  if (m_Asids[j] > 0)
60  bulldoze(j);
61  m_Asids[j]++;
62  m_Mutex.release();
63  return j;
64  }
65  // No? Then release the lock and keep searching.
66  m_Mutex.release();
67  }
68  }
69  }
70  // If we got here then we didn't find any asid with less than a contention
71  // of 10. This is really really bad. Panic here.
72  panic("Too many processes running, not enough ASIDs!");
73 }
74 
76 {
77  // Grab the lock.
78  m_Mutex.acquire();
79  // Sanity check...
80  if (m_Asids[asid] == 0)
81  ERROR("returnAsid called on an ASID that hasn't been allocated!");
82  m_Asids[asid]--;
83  // Release the lock.
84  m_Mutex.release();
85 }
86 
88 {
89  // TODO:: implement
90 }
Mutex m_Mutex
Definition: AsidManager.h:73
bool acquire(size_t n=1, size_t timeoutSecs=0, size_t timeoutUsecs=0)
Definition: Semaphore.h:62
void bulldoze(Asid asid)
Definition: AsidManager.cc:87
static AsidManager m_Instance
Definition: AsidManager.h:76
static AsidManager & instance()
Definition: AsidManager.cc:26
void release(size_t n=1)
Definition: Semaphore.cc:239
Asid obtainAsid()
Definition: AsidManager.cc:39
void returnAsid(Asid asid)
Definition: AsidManager.cc:75
uint32_t m_Asids[NUM_ASID]
Definition: AsidManager.h:70
uint8_t Asid
Definition: AsidManager.h:48
#define ERROR(text)
Definition: Log.h:82
void EXPORTED_PUBLIC panic(const char *msg) NORETURN
Definition: panic.cc:121