forked from nieklinnenbank/FreeNOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.cpp
More file actions
228 lines (189 loc) · 5.31 KB
/
Process.cpp
File metadata and controls
228 lines (189 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright (C) 2015 Niek Linnenbank
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <FreeNOS/System.h>
#include <FreeNOS/API.h>
#include <Index.h>
#include <MemoryBlock.h>
#include <MemoryChannel.h>
#include <SplitAllocator.h>
#include "Process.h"
#include "ProcessEvent.h"
Process::Process(ProcessID id, Address entry, bool privileged, const MemoryMap &map)
: m_id(id), m_map(map), m_shares(id)
{
m_state = Stopped;
m_kernelStack = 0;
m_userStack = 0;
m_pageDirectory = 0;
m_parent = 0;
m_waitId = 0;
m_wakeups = 0;
m_entry = entry;
m_privileged = privileged;
m_memoryContext = ZERO;
m_kernelChannel = new MemoryChannel;
MemoryBlock::set(&m_sleepTimer, 0, sizeof(m_sleepTimer));
}
Process::~Process()
{
delete m_kernelChannel;
if (m_memoryContext)
{
m_memoryContext->releaseRegion(MemoryMap::UserData);
m_memoryContext->releaseRegion(MemoryMap::UserHeap);
m_memoryContext->releaseRegion(MemoryMap::UserStack);
m_memoryContext->releaseRegion(MemoryMap::UserPrivate);
delete m_memoryContext;
}
}
ProcessID Process::getID() const
{
return m_id;
}
ProcessID Process::getParent() const
{
return m_parent;
}
ProcessID Process::getWait() const
{
return m_waitId;
}
Process::State Process::getState() const
{
return m_state;
}
ProcessShares & Process::getShares()
{
return m_shares;
}
const Timer::Info * Process::getSleepTimer() const
{
return &m_sleepTimer;
}
Address Process::getPageDirectory() const
{
return m_pageDirectory;
}
Address Process::getUserStack() const
{
return m_userStack;
}
Address Process::getKernelStack() const
{
return m_kernelStack;
}
MemoryContext * Process::getMemoryContext()
{
return m_memoryContext;
}
bool Process::isPrivileged() const
{
return m_privileged;
}
void Process::setState(Process::State st)
{
m_state = st;
}
void Process::setParent(ProcessID id)
{
m_parent = id;
}
void Process::setWait(ProcessID id)
{
m_waitId = id;
}
void Process::setSleepTimer(const Timer::Info *sleepTimer)
{
MemoryBlock::copy(&m_sleepTimer, sleepTimer, sizeof(m_sleepTimer));
}
void Process::setPageDirectory(Address addr)
{
m_pageDirectory = addr;
}
void Process::setUserStack(Address addr)
{
m_userStack = addr;
}
void Process::setKernelStack(Address addr)
{
m_kernelStack = addr;
}
Process::Result Process::raiseEvent(ProcessEvent *event)
{
// Write the message. Be sure to flush the caches because
// the kernel has mapped the channel pages separately in low memory.
m_kernelChannel->write(event);
m_kernelChannel->flush();
// Wakeup the Process, if needed
return wakeup();
}
Process::Result Process::initialize()
{
Memory::Range range;
Address paddr, vaddr;
Arch::Cache cache;
// Allocate two pages for the kernel event channel
if (Kernel::instance->getAllocator()->allocateLow(PAGESIZE*2, &paddr) != Allocator::Success)
return OutOfMemory;
// Translate to virtual address in kernel low memory
vaddr = (Address) Kernel::instance->getAllocator()->toVirtual(paddr);
MemoryBlock::set((void *)vaddr, 0, PAGESIZE*2);
cache.cleanData(vaddr);
cache.cleanData(vaddr + PAGESIZE);
// Map data and feedback pages in userspace
range.phys = paddr;
range.access = Memory::User | Memory::Readable;
range.size = PAGESIZE * 2;
m_memoryContext->findFree(range.size, MemoryMap::UserPrivate, &range.virt);
m_memoryContext->mapRange(&range);
// Remap the feedback page with write permissions
m_memoryContext->unmap(range.virt + PAGESIZE);
m_memoryContext->map(range.virt + PAGESIZE,
range.phys + PAGESIZE, Memory::User | Memory::Readable | Memory::Writable);
// Create shares entry
m_shares.setMemoryContext(m_memoryContext);
m_shares.createShare(KERNEL_PID, Kernel::instance->getCoreInfo()->coreId, 0, range.virt, range.size);
// Setup the kernel event channel
m_kernelChannel->setMode(Channel::Producer);
m_kernelChannel->setMessageSize(sizeof(ProcessEvent));
m_kernelChannel->setVirtual(vaddr, vaddr + PAGESIZE);
return Success;
}
Process::Result Process::wakeup()
{
m_wakeups++;
if (m_state != Running)
m_state = Ready;
MemoryBlock::set(&m_sleepTimer, 0, sizeof(m_sleepTimer));
return Success;
}
Process::Result Process::sleep(Timer::Info *timer)
{
if (!m_wakeups)
{
m_state = Sleeping;
if (timer)
MemoryBlock::copy(&m_sleepTimer, timer, sizeof(m_sleepTimer));
return Success;
}
m_wakeups = 0;
return WakeupPending;
}
bool Process::operator==(Process *proc)
{
return proc->getID() == m_id;
}