Skip to content

Commit

Permalink
Add a VMO pointer to VNode.
Browse files Browse the repository at this point in the history
This way, if anyone tries to map an already mapped file, we share the VMO.
  • Loading branch information
awesomekling committed Nov 8, 2018
1 parent 862f108 commit 3b2dcd5
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 10 deletions.
2 changes: 1 addition & 1 deletion AK/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Function<Out(In...)> {
};

template<typename CallableType>
class CallableWrapper : public CallableWrapperBase {
class CallableWrapper final : public CallableWrapperBase {
public:
explicit CallableWrapper(CallableType&& callable)
: m_callable(move(callable))
Expand Down
5 changes: 3 additions & 2 deletions AK/StdLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ inline T max(const T& a, const T& b)
}


template<typename T>
static inline T ceilDiv(T a, T b)
template<typename T, typename U>
static inline T ceilDiv(T a, U b)
{
static_assert(sizeof(T) == sizeof(U));
T result = a / b;
if ((a % b) != 0)
++result;
Expand Down
20 changes: 16 additions & 4 deletions Kernel/MemoryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ Region* MemoryManager::region_from_laddr(Process& process, LinearAddress laddr)
return region.ptr();
}
kprintf("%s(%u) Couldn't find region for L%x\n", process.name().characters(), process.pid(), laddr.get());
process.dumpRegions();
ASSERT_NOT_REACHED();
return nullptr;
}

bool MemoryManager::copy_on_write(Process& process, Region& region, unsigned page_index_in_region)
Expand Down Expand Up @@ -292,7 +291,10 @@ PageFaultResponse MemoryManager::handle_page_fault(const PageFault& fault)
dbgprintf("MM: handle_page_fault(%w) at L%x\n", fault.code(), fault.laddr().get());
#endif
auto* region = region_from_laddr(*current, fault.laddr());
ASSERT(region);
if (!region) {
kprintf("NP(error) fault at invalid address L%x\n", fault.laddr().get());
return PageFaultResponse::ShouldCrash;
}
auto page_index_in_region = region->page_index_from_address(fault.laddr());
if (fault.is_not_present()) {
if (region->vmo().vnode()) {
Expand Down Expand Up @@ -640,7 +642,12 @@ void PhysicalPage::return_to_freelist()

RetainPtr<VMObject> VMObject::create_file_backed(RetainPtr<VirtualFileSystem::Node>&& vnode, size_t size)
{
return adopt(*new VMObject(move(vnode), size));
InterruptDisabler disabler;
if (vnode->vmo())
return static_cast<VMObject*>(vnode->vmo());
auto vmo = adopt(*new VMObject(move(vnode), size));
vmo->vnode()->set_vmo(vmo.ptr());
return vmo;
}

RetainPtr<VMObject> VMObject::create_anonymous(size_t size)
Expand Down Expand Up @@ -679,6 +686,11 @@ VMObject::VMObject(RetainPtr<VirtualFileSystem::Node>&& vnode, size_t size)

VMObject::~VMObject()
{
InterruptDisabler disabler;
if (m_vnode) {
ASSERT(m_vnode->vmo() == this);
m_vnode->set_vmo(nullptr);
}
}

int Region::commit(Process& process)
Expand Down
9 changes: 7 additions & 2 deletions Kernel/ProcFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ByteBuffer procfs$pid_vm(Process& process)
{
ProcessInspectionScope scope(process);
char* buffer;
auto stringImpl = StringImpl::createUninitialized(80 + process.regionCount() * 80 + 4096, buffer);
auto stringImpl = StringImpl::createUninitialized(80 + process.regionCount() * 160 + 4096, buffer);
memset(buffer, 0, stringImpl->length());
char* ptr = buffer;
ptr += ksprintf(ptr, "BEGIN END SIZE NAME\n");
Expand All @@ -59,12 +59,17 @@ ByteBuffer procfs$pid_vm(Process& process)
region->linearAddress.offset(region->size - 1).get(),
region->size,
region->name.characters());
ptr += ksprintf(ptr, "VMO: %s \"%s\" @ %x(%u)\n",
region->vmo().is_anonymous() ? "anonymous" : "file-backed",
region->vmo().name().characters(),
&region->vmo(),
region->vmo().retainCount());
for (size_t i = 0; i < region->vmo().page_count(); ++i) {
auto& physical_page = region->vmo().physical_pages()[i];
ptr += ksprintf(ptr, "P%x%s(%u) ",
physical_page ? physical_page->paddr().get() : 0,
region->cow_map.get(i) ? "!" : "",
physical_page->retain_count()
physical_page ? physical_page->retain_count() : 0
);
}
ptr += ksprintf(ptr, "\n");
Expand Down
1 change: 0 additions & 1 deletion Kernel/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ void init()
procfs->initialize();

Process::initialize();

Process::create_kernel_process(init_stage2, "init_stage2");

Scheduler::pick_next();
Expand Down
25 changes: 25 additions & 0 deletions Userland/sh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,27 @@ static int sh_mf(int, const char**)
return 0;
}

static int sh_mp(int, const char**)
{
int rc;
int fd = open("/kernel.map", O_RDONLY);
if (fd < 0) {
perror("open(/kernel.map)");
return 1;
}
printf("opened /kernel.map, calling mmap...\n");
byte* data = (byte*)mmap(nullptr, getpagesize() * 10, PROT_READ, MAP_PRIVATE, fd, 0);
if (data == MAP_FAILED) {
perror("mmap()");
return 1;
}
printf("mapped file @ %p\n", data);
printf("contents: %c%c%c%c%c%c%c...\n", data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);

printf("leaving it open :)\n");
return 0;
}

static int sh_exit(int, const char**)
{
printf("Good-bye!\n");
Expand Down Expand Up @@ -221,6 +242,10 @@ static bool handle_builtin(int argc, const char** argv, int& retval)
retval = sh_mf(argc, argv);
return true;
}
if (!strcmp(argv[0], "mp")) {
retval = sh_mp(argc, argv);
return true;
}
if (!strcmp(argv[0], "fork")) {
retval = sh_fork(argc, argv);
return true;
Expand Down
3 changes: 3 additions & 0 deletions VirtualFileSystem/VirtualFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ auto VirtualFileSystem::allocateNode() -> RetainPtr<Node>
ASSERT(node->retainCount == 0);
node->retainCount = 1;
node->m_vfs = this;
node->m_vmo = nullptr;
return adopt(*node);
}

Expand All @@ -197,6 +198,8 @@ void VirtualFileSystem::freeNode(Node* node)
m_device2vnode.remove(encodedDevice(node->m_characterDevice->major(), node->m_characterDevice->minor()));
node->m_characterDevice = nullptr;
}
node->m_vfs = nullptr;
node->m_vmo = nullptr;
m_nodeFreeList.append(move(node));
}

Expand Down
4 changes: 4 additions & 0 deletions VirtualFileSystem/VirtualFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,16 @@ class VirtualFileSystem {
VirtualFileSystem* vfs() { return m_vfs; }
const VirtualFileSystem* vfs() const { return m_vfs; }

void* vmo() { return m_vmo; }
void set_vmo(void* vmo) { m_vmo = vmo; }

private:
friend class VirtualFileSystem;
VirtualFileSystem* m_vfs { nullptr };
unsigned retainCount { 0 };
CharacterDevice* m_characterDevice { nullptr };
mutable InodeMetadata m_cachedMetadata;
void* m_vmo { nullptr };
};

static VirtualFileSystem& the() PURE;
Expand Down

0 comments on commit 3b2dcd5

Please sign in to comment.