-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathms_create.c
113 lines (102 loc) · 4.15 KB
/
ms_create.c
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
/**
* Copyright 2023, Philip Meulengracht
*
* 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 <arch/mmu.h>
#include <debug.h>
#include <handle.h>
#include <string.h>
#include "private.h"
oserr_t
CreateMemorySpace(
_In_ unsigned int flags,
_Out_ uuid_t* handleOut)
{
MemorySpace_t* memorySpace;
oserr_t oserr;
TRACE("CreateMemorySpace(flags=0x%x)", flags);
memorySpace = MemorySpaceNew(flags);
if (!memorySpace) {
return OS_EOOM;
}
// We must handle two cases here, either we inherit the kernels address-space, or we are
// inheritting/creating a new userspace address-space. If we are inheritting the kernel
// address-space, we clone it as we still need TLS data-areas in each kernel thread.
if (flags == MEMORY_SPACE_INHERIT) {
// When cloning kernel memory spaces, we initially just copy the platform block
// for the new memory space, and then let the Platfrom call correct anything.
if (GetCurrentMemorySpace() != NULL) {
memcpy(
&memorySpace->PlatformData,
&GetCurrentMemorySpace()->PlatformData,
sizeof(PlatformMemoryBlock_t)
);
}
// It doesn't matter which parent we take, they all map the exact same kernel segments
// so just pass in the current memory space
oserr = MmuCloneVirtualSpace(
GetCurrentMemorySpace(),
memorySpace,
(flags & MEMORY_SPACE_INHERIT) ? 1 : 0
);
if (oserr != OS_EOK) {
return oserr;
}
*handleOut = GetCurrentMemorySpaceHandle();
} else if (flags & MEMORY_SPACE_APPLICATION) {
MemorySpace_t* parent = NULL;
// Parent must be the uppermost instance of the address-space
// of the process. Only to the point of not having kernel as parent
if (flags & MEMORY_SPACE_INHERIT) {
parent = GetCurrentMemorySpace();
if (parent != GetDomainMemorySpace()) {
if (parent->ParentHandle != UUID_INVALID) {
memorySpace->ParentHandle = parent->ParentHandle;
memorySpace->Context = parent->Context;
parent = (MemorySpace_t*)LookupHandleOfType(
parent->ParentHandle, HandleTypeMemorySpace);
} else {
memorySpace->ParentHandle = GetCurrentMemorySpaceHandle();
memorySpace->Context = parent->Context;
}
// Add a reference and copy data
AcquireHandleOfType(
memorySpace->ParentHandle,
HandleTypeMemorySpace,
NULL
);
memcpy(&memorySpace->PlatformData, &parent->PlatformData, sizeof(PlatformMemoryBlock_t));
} else {
parent = NULL;
}
}
// If we are root, create the memory bitmaps
if (memorySpace->ParentHandle == UUID_INVALID) {
memorySpace->Context = MSContextNew();
}
oserr = MmuCloneVirtualSpace(parent, memorySpace, (flags & MEMORY_SPACE_INHERIT) ? 1 : 0);
if (oserr != OS_EOK) {
return oserr;
}
*handleOut = CreateHandle(
HandleTypeMemorySpace,
(HandleDestructorFn)MemorySpaceDelete,
memorySpace
);
} else {
FATAL(FATAL_SCOPE_KERNEL, "Invalid flags parsed in CreateMemorySpace 0x%" PRIxIN "", flags);
}
return OS_EOK;
}