FreeNOS
Process.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <FreeNOS/System.h>
19#include <FreeNOS/API.h>
20#include <MemoryBlock.h>
21#include <MemoryChannel.h>
22#include <SplitAllocator.h>
23#include "Process.h"
24#include "ProcessEvent.h"
25
26Process::Process(ProcessID id, Address entry, bool privileged, const MemoryMap &map)
27 : m_id(id), m_map(map), m_shares(id)
28{
30 m_parent = 0;
31 m_waitId = 0;
32 m_waitResult = 0;
33 m_wakeups = 0;
34 m_entry = entry;
35 m_privileged = privileged;
39}
40
59
61{
62 return m_id;
63}
64
66{
67 return m_parent;
68}
69
71{
72 return m_waitId;
73}
74
76{
77 return m_waitResult;
78}
79
81{
82 return m_state;
83}
84
89
91{
92 return m_sleepTimer;
93}
94
99
101{
102 return m_privileged;
103}
104
106{
107 m_parent = id;
108}
109
111{
112 if (m_state != Ready)
113 {
114 ERROR("Process ID " << m_id << " has invalid state: " << (uint) m_state);
115 return InvalidArgument;
116 }
117
119 m_waitId = id;
120
121 return Success;
122}
123
125{
126 if (m_state != Waiting)
127 {
128 ERROR("PID " << m_id << " has invalid state: " << (uint) m_state);
129 return InvalidArgument;
130 }
131
132 m_waitResult = result;
133 m_state = Ready;
134 return Success;
135}
136
138{
139 if (m_state != Ready && m_state != Sleeping && m_state != Stopped)
140 {
141 ERROR("PID " << m_id << " has invalid state: " << (uint) m_state);
142 return InvalidArgument;
143 }
144
146 return Success;
147}
148
150{
151 if (m_state != Stopped)
152 {
153 ERROR("PID " << m_id << " has invalid state: " << (uint) m_state);
154 return InvalidArgument;
155 }
156
157 m_state = Ready;
158 return Success;
159}
160
162{
163 // Write the message. Be sure to flush the caches because
164 // the kernel has mapped the channel pages separately in low memory.
165 m_kernelChannel->write(event);
167
168 // Wakeup the Process, if needed
169 return wakeup();
170}
171
173{
174 Memory::Range range;
175 Arch::Cache cache;
176 Allocator::Range allocPhys, allocVirt;
177
178 // Create new kernel event channel object
180 if (!m_kernelChannel)
181 {
182 ERROR("failed to allocate kernel event channel object");
183 return OutOfMemory;
184 }
185
186 // Allocate two pages for the kernel event channel
187 allocPhys.address = 0;
188 allocPhys.size = PAGESIZE * 2;
189 allocPhys.alignment = PAGESIZE;
190
191 if (Kernel::instance()->getAllocator()->allocate(allocPhys, allocVirt) != Allocator::Success)
192 {
193 ERROR("failed to allocate kernel event channel pages");
194 return OutOfMemory;
195 }
196
197 // Initialize pages with zeroes
198 MemoryBlock::set((void *)allocVirt.address, 0, PAGESIZE*2);
199 cache.cleanData(allocVirt.address);
200 cache.cleanData(allocVirt.address + PAGESIZE);
201
202 // Map data and feedback pages in userspace
203 range.phys = allocPhys.address;
205 range.size = PAGESIZE * 2;
208
209 // Remap the feedback page with write permissions
213
214 // Create shares entry
216 m_shares.createShare(KERNEL_PID, Kernel::instance()->getCoreInfo()->coreId, 0, range.virt, range.size);
217
218 // Setup the kernel event channel
219 m_kernelChannel->setVirtual(allocVirt.address, allocVirt.address + PAGESIZE);
220
221 return Success;
222}
223
225{
226 // This process might be just about to call sleep().
227 // When another process is asking to wakeup this Process
228 // such that it can receive an IPC message, we must guarantee
229 // that the next sleep will be skipped.
230 m_wakeups++;
231
232 if (m_state == Sleeping)
233 {
234 m_state = Ready;
236 return Success;
237 }
238 else
239 {
240 return WakeupPending;
241 }
242}
243
244Process::Result Process::sleep(const Timer::Info *timer, bool ignoreWakeups)
245{
246 if (m_state != Ready)
247 {
248 ERROR("PID " << m_id << " has invalid state: " << (uint) m_state);
249 return InvalidArgument;
250 }
251
252 if (!m_wakeups || ignoreWakeups)
253 {
255
256 if (timer)
258
259 return Success;
260 }
261 m_wakeups = 0;
262 return WakeupPending;
263}
264
266{
267 return proc->getID() == m_id;
268}
u32 entry[]
Definition IntelACPI.h:1
u8 coreId
Definition IntelACPI.h:1
ARMv6 cache management implementation.
Definition ARMCacheV6.h:43
virtual Result cleanData(Address addr)
Clean one data page.
Definition Cache.cpp:20
@ Producer
Definition Channel.h:58
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
Unidirectional point-to-point channel using shared memory.
virtual Result flush()
Flush message buffers.
virtual Result write(const void *buffer)
Write a message.
Result setVirtual(const Address data, const Address feedback, const bool hardReset=true)
Set memory pages by virtual address.
Virtual memory abstract interface.
virtual Result unmap(Address virt)=0
Unmap a virtual address.
virtual Result releaseSection(const Memory::Range &range, const bool tablesOnly=false)=0
Release memory sections.
virtual Result findFree(Size size, MemoryMap::Region region, Address *virt) const
Find unused memory.
virtual Result map(Address virt, Address phys, Memory::Access access)=0
Map a physical page to a virtual address.
virtual Result mapRangeContiguous(Memory::Range *range)
Map a range of contiguous physical pages to virtual addresses.
Describes virtual memory map layout.
Definition MemoryMap.h:39
@ UserData
< User program data from libexec, e.g.
Definition MemoryMap.h:56
@ UserArgs
< Used for copying program arguments and file descriptors
Definition MemoryMap.h:61
@ UserStack
< User stack
Definition MemoryMap.h:58
@ UserShare
< User shared dynamic memory mappings
Definition MemoryMap.h:60
@ UserPrivate
< User private dynamic memory mappings
Definition MemoryMap.h:59
@ UserHeap
< User heap
Definition MemoryMap.h:57
Memory::Range range(Region region) const
Get memory range for the given region.
Definition MemoryMap.cpp:36
Manages memory shares for a Process.
Result setMemoryContext(MemoryContext *context)
Set MemoryContext.
Result createShare(ProcessShares &instance, MemoryShare *share)
Represents a process which may run on the host.
Definition Process.h:45
const Timer::Info & getSleepTimer() const
Get sleep timer.
Definition Process.cpp:90
MemoryContext * m_memoryContext
MMU memory context.
Definition Process.h:271
Result wait(ProcessID id)
Let Process wait for other Process to terminate.
Definition Process.cpp:110
State
Represents the execution state of the Process.
Definition Process.h:67
@ Stopped
Definition Process.h:71
@ Ready
Definition Process.h:68
@ Sleeping
Definition Process.h:69
@ Waiting
Definition Process.h:70
Size m_wakeups
Number of wakeups received.
Definition Process.h:274
const ProcessID m_id
Process Identifier.
Definition Process.h:247
virtual Result join(const uint result)
Complete waiting for another Process.
Definition Process.cpp:124
uint getWaitResult() const
Get wait result.
Definition Process.cpp:75
Result stop()
Stop execution of this process.
Definition Process.cpp:137
MemoryChannel * m_kernelChannel
Channel for sending kernel events to the Process.
Definition Process.h:287
State m_state
Current process status.
Definition Process.h:253
ProcessID getWait() const
Get Wait ID.
Definition Process.cpp:70
Result resume()
Resume execution when this process is stopped.
Definition Process.cpp:149
Result
Result codes.
Definition Process.h:55
@ Success
Definition Process.h:56
@ InvalidArgument
Definition Process.h:57
@ OutOfMemory
Definition Process.h:59
@ WakeupPending
Definition Process.h:60
Result sleep(const Timer::Info *timer, bool ignoreWakeups)
Stops the process for executing until woken up.
Definition Process.cpp:244
State getState() const
Retrieves the current state.
Definition Process.cpp:80
virtual Result initialize()
Initialize the Process.
Definition Process.cpp:172
ProcessShares m_shares
Contains virtual memory shares between this process and others.
Definition Process.h:284
ProcessID getID() const
Retrieve our ID number.
Definition Process.cpp:60
void setParent(ProcessID id)
Set parent process ID.
Definition Process.cpp:105
MemoryMap m_map
Virtual memory layout.
Definition Process.h:268
ProcessID getParent() const
Retrieve our parent ID.
Definition Process.cpp:65
MemoryContext * getMemoryContext()
Get MMU memory context.
Definition Process.cpp:95
virtual ~Process()
Destructor function.
Definition Process.cpp:41
bool m_privileged
Privilege level.
Definition Process.h:262
Process(ProcessID id, Address entry, bool privileged, const MemoryMap &map)
Constructor function.
Definition Process.cpp:26
ProcessShares & getShares()
Get process shares.
Definition Process.cpp:85
ProcessID m_waitId
Waits for exit of this Process.
Definition Process.h:256
Result wakeup()
Prevent process from sleeping.
Definition Process.cpp:224
Result raiseEvent(const struct ProcessEvent *event)
Raise kernel event.
Definition Process.cpp:161
bool operator==(Process *proc)
Compare two processes.
Definition Process.cpp:265
uint m_waitResult
Wait exit result of the other Process.
Definition Process.h:259
bool isPrivileged() const
Get privilege.
Definition Process.cpp:100
Address m_entry
Entry point of the program.
Definition Process.h:265
ProcessID m_parent
Parent process.
Definition Process.h:250
Timer::Info m_sleepTimer
Sleep timer value.
Definition Process.h:281
static Kernel * instance()
Retrieve the instance.
Definition Singleton.h:86
#define KERNEL_PID
Definition ProcessID.h:36
#define PAGESIZE
ARM uses 4K pages.
Definition ARMConstant.h:97
u32 ProcessID
Process Identification Number.
Definition Types.h:140
unsigned long Address
A memory address.
Definition Types.h:131
#define ERROR(msg)
Output an error message.
Definition Log.h:61
unsigned int uint
Unsigned integer number.
Definition Types.h:44
#define ZERO
Zero value.
Definition Macros.h:43
@ User
Definition Memory.h:44
@ Readable
Definition Memory.h:41
@ Writable
Definition Memory.h:42
Describes a range of memory.
Definition Allocator.h:66
Size alignment
Alignment in bytes or ZERO for default alignment.
Definition Allocator.h:69
Address address
Starting address of the memory range.
Definition Allocator.h:67
Size size
Amount of memory in bytes.
Definition Allocator.h:68
Memory range.
Definition Memory.h:56
Size size
Size in number of bytes.
Definition Memory.h:59
Address phys
Physical address.
Definition Memory.h:58
Address virt
Virtual address.
Definition Memory.h:57
Access access
Page access flags.
Definition Memory.h:60
Represents a process which may run on the host.
Timer information structure.
Definition Timer.h:43