The Pedigree Project 0.1
UserMemoryPolicy.h
1/*
2 * Copyright (c) 2026, Pedigree Developers
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted.
6 */
7
8#ifndef PEDIGREE_KERNEL_PROCESSOR_USERMEMORYPOLICY_H
9#define PEDIGREE_KERNEL_PROCESSOR_USERMEMORYPOLICY_H
10
11#include "pedigree/kernel/compiler.h"
12#include "pedigree/kernel/process/TerminationDeferral.h"
13#include "pedigree/kernel/process/Uninterruptible.h"
14#include "pedigree/kernel/processor/types.h"
15#include "pedigree/kernel/utilities/Pointers.h"
16
18
19enum class MemoryLockMode { None, Eager, OnFault };
20enum class PopulationStatus { Success, Inaccessible, NoMemory, IoError };
21enum class MemoryLockStatus {
22 Success,
23 InvalidRange,
24 Unmapped,
25 Unsupported,
26 NoMemory,
27 LockLimit,
28 PopulationNoMemory,
29 PopulationInaccessible,
30 PopulationIoError,
31};
32
34 size_t managedPages = 0;
35 size_t rawPages = 0;
36};
37
39class EXPORTED_PUBLIC MemoryLockAccount {
40 public:
41 virtual ~MemoryLockAccount() = default;
42 virtual bool permitsTotalPages(size_t total, bool privileged) const = 0;
43 virtual bool permitsLocking(bool) const {
44 return true;
45 }
46 MemoryLockCharge charge() const {
47 return m_Charge;
48 }
49 MemoryLockMode futureMode() const {
50 return m_Future;
51 }
52 void publish(MemoryLockCharge charge, MemoryLockMode future) {
53 m_Charge = charge;
54 m_Future = future;
55 }
56
57 private:
58 MemoryLockCharge m_Charge;
59 MemoryLockMode m_Future = MemoryLockMode::None;
60};
61
65 uintptr_t base;
66 size_t length;
67};
68
69class EXPORTED_PUBLIC PreparedMemoryLock {
70 public:
71 virtual ~PreparedMemoryLock() = default;
72 virtual size_t removedPages() const = 0;
73 virtual size_t addedPages() const = 0;
74 virtual size_t coveredPages() const = 0;
75 virtual size_t eligiblePages() const = 0;
76 virtual size_t removedRangeCount() const {
77 return 0;
78 }
79 virtual const MemoryLockRange* removedRanges() const {
80 return nullptr;
81 }
82 virtual void commit() = 0;
83 virtual PopulationStatus populate() = 0;
84};
85
87class EXPORTED_PUBLIC UserMemoryPolicy {
88 public:
89 virtual ~UserMemoryPolicy() = default;
90 virtual bool callerHasMemoryLockPrivilege() const = 0;
91 virtual bool overlapsManagedMemory(VirtualAddressSpace& space, uintptr_t base,
92 size_t length) const = 0;
93 virtual void enterOperation() = 0;
94 virtual void leaveOperation() = 0;
95};
96
97class EXPORTED_PUBLIC UserMemoryOperation {
98 public:
101 bool privileged() const {
102 return m_Privileged;
103 }
104
105 private:
106 NOT_COPYABLE_OR_ASSIGNABLE(UserMemoryOperation);
107 Uninterruptible m_EventDeferral;
108 TerminationDeferral m_TerminationDeferral;
109 UserMemoryPolicy* m_Policy;
110 bool m_Privileged;
111};
112
114 enum class Kind { Heap, Stack };
115 uint64_t id;
116 uintptr_t base;
117 size_t length;
118 Kind kind;
119 bool privateWritable;
120};
121
123class EXPORTED_PUBLIC RawUserMemory {
124 public:
125 explicit RawUserMemory(VirtualAddressSpace& space);
127 uint64_t nextRegionId();
128 bool completeInventory() const;
129 void setCompleteInventory(bool complete);
131 bool covers(uintptr_t base, size_t length) const;
132 bool hasLockedMemory(uintptr_t base, size_t length) const;
133 MemoryLockStatus prepareChange(const UserRegion* previous, const UserRegion* replacement,
135 MemoryLockStatus prepareLocks(uintptr_t base, size_t length, MemoryLockMode mode,
137 MemoryLockStatus prepareAllLocks(MemoryLockMode mode, UniquePointer<PreparedMemoryLock>& result);
140 MemoryLockStatus prepareReplacement(uintptr_t base, size_t length,
142 MemoryLockStatus cloneInto(RawUserMemory& target);
143 size_t retireRegion(uint64_t id);
144 void clear();
145
146 private:
147 NOT_COPYABLE_OR_ASSIGNABLE(RawUserMemory);
148 struct State;
149 class Plan;
150 VirtualAddressSpace& m_Space;
151 UniquePointer<State> m_State;
152};
153
154#endif