The Pedigree Project  0.1
x86_common/ProcessorInformation.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 "pedigree/kernel/processor/x86_common/ProcessorInformation.h"
21 #include "pedigree/kernel/process/InfoBlock.h"
22 #include "pedigree/kernel/process/PerProcessorScheduler.h"
23 #include "pedigree/kernel/process/Process.h"
24 #include "pedigree/kernel/process/Thread.h"
25 #include "pedigree/kernel/processor/VirtualAddressSpace.h"
26 #include "pedigree/kernel/processor/types.h"
27 #include "pedigree/kernel/utilities/new"
28 
29 #if defined(X86)
30 #include "pedigree/kernel/processor/x86/tss.h"
31 #else
32 #include "pedigree/kernel/processor/x64/tss.h"
33 #endif
34 
39 {
41  return *m_VirtualAddressSpace;
42  else
44 }
48  VirtualAddressSpace &virtualAddressSpace)
49 {
50  m_VirtualAddressSpace = &virtualAddressSpace;
51 }
52 
56 {
57  m_TssSelector = TssSelector;
58 }
62 {
63  m_Tss = reinterpret_cast<TaskStateSegment *>(Tss);
64 }
68 {
69  return m_TssSelector;
70 }
74 {
75  return reinterpret_cast<void *>(m_Tss);
76 }
79 {
80  return m_TlsSelector;
81 }
84 {
85  m_TlsSelector = tls;
86 }
87 
88 uintptr_t X86CommonProcessorInformation::getKernelStack() const
89 {
90 #if defined(X86)
91  return m_Tss->esp0;
92 #else
93  return m_Tss->rsp0;
94 #endif
95 }
96 void X86CommonProcessorInformation::setKernelStack(uintptr_t stack)
97 {
98 #if defined(X86)
99  m_Tss->esp0 = stack;
100 #else
101  m_Tss->rsp0 = stack;
102  // Can't use Procesor::writeMachineSpecificRegister as Processor is
103  // undeclared here!
104  uint32_t eax = stack, edx = stack >> 32;
105  asm volatile("wrmsr" ::"a"(eax), "d"(edx), "c"(0xc0000102));
106 #endif
107 }
108 
109 Thread *X86CommonProcessorInformation::getCurrentThread() const
110 {
111  return m_pCurrentThread;
112 }
113 
114 void X86CommonProcessorInformation::setCurrentThread(Thread *pThread)
115 {
116  m_pCurrentThread = pThread;
117  InfoBlockManager::instance().setPid(pThread->getParent()->getId());
118 }
119 
120 PerProcessorScheduler &X86CommonProcessorInformation::getScheduler()
121 {
122  // Allocate the scheduler lazily.
123  if (m_Scheduler == nullptr)
124  {
126  }
127  return *m_Scheduler;
128 }
129 
131  ProcessorId processorId, uint8_t apicId)
132  : m_ProcessorId(processorId), m_TssSelector(0), m_Tss(0),
133  m_VirtualAddressSpace(&VirtualAddressSpace::getKernelAddressSpace()),
134  m_LocalApicId(apicId), m_pCurrentThread(0), m_Scheduler(nullptr),
135  m_TlsSelector(0)
136 {
137 }
140 {
141  delete m_Scheduler;
142 }
143 
144 void X86CommonProcessorInformation::setIds(
145  ProcessorId processorId, uint8_t apicId)
146 {
147  m_ProcessorId = processorId;
148  m_LocalApicId = apicId;
149 }
size_t getId()
Definition: Process.h:108
static EXPORTED_PUBLIC VirtualAddressSpace & getKernelAddressSpace()
VirtualAddressSpace & getVirtualAddressSpace() const
size_t ProcessorId
Process * getParent() const
Definition: Thread.h:181
Definition: Thread.h:54
void setVirtualAddressSpace(VirtualAddressSpace &virtualAddressSpace)