Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetMemoryInformation request to kerncfg/posix #582

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions kernel/thor/generic/kerncfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <thor-internal/stream.hpp>
#include <thor-internal/timer.hpp>
#include <thor-internal/mbus.hpp>
#include <thor-internal/physical.hpp>

#include <bragi/helpers-frigg.hpp>
#include <bragi/helpers-all.hpp>
Expand Down Expand Up @@ -71,6 +72,23 @@ struct KerncfgBusObject : private KernelBusObject {
auto cmdlineError = co_await SendBufferSender{lane, std::move(cmdlineBuffer)};
if(cmdlineError != Error::success)
co_return cmdlineError;
}else if(preamble.id() == bragi::message_id<managarm::kerncfg::GetMemoryInformationRequest>) {
auto req = bragi::parse_head_only<managarm::kerncfg::GetMemoryInformationRequest>(reqBuffer, *kernelAlloc);

if (!req)
co_return Error::protocolViolation;

managarm::kerncfg::GetMemoryInformationResponse<KernelAlloc> resp(*kernelAlloc);
resp.set_error(managarm::kerncfg::Error::SUCCESS);
resp.set_total_usable_memory(physicalAllocator->numTotalPages());
resp.set_available_memory(physicalAllocator->numFreePages());
resp.set_memory_unit(kPageSize);

frg::unique_memory<KernelAlloc> respBuffer{*kernelAlloc, resp.size_of_head()};
bragi::write_head_only(resp, respBuffer);
auto respError = co_await SendBufferSender{lane, std::move(respBuffer)};
if(respError != Error::success)
co_return respError;
}else{
managarm::kerncfg::SvrResponse<KernelAlloc> resp(*kernelAlloc);
resp.set_error(managarm::kerncfg::Error::ILLEGAL_REQUEST);
Expand Down
4 changes: 4 additions & 0 deletions posix/subsystem/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ namespace {
helix::UniqueLane kerncfgLane;
};

helix::UniqueLane &getKerncfgLane() {
return kerncfgLane;
}

struct CmdlineNode final : public procfs::RegularNode {
async::result<std::string> show() override {

Expand Down
33 changes: 33 additions & 0 deletions posix/subsystem/src/requests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <bragi/helpers-std.hpp>
#include <posix.bragi.hpp>
#include <kerncfg.bragi.hpp>

#include "debug-options.hpp"

Expand Down Expand Up @@ -3271,6 +3272,38 @@ async::result<void> serveRequests(std::shared_ptr<Process> self,
helix_ng::sendBuffer(affinity.data(), affinity.size())
);
HEL_CHECK(sendResp.error());
}else if(preamble.id() == managarm::posix::GetMemoryInformationRequest::message_id) {
auto req = bragi::parse_head_only<managarm::posix::GetMemoryInformationRequest>(recv_head);

if (!req) {
std::cout << "posix: Rejecting request due to decoding failure" << std::endl;
break;
}

if(logRequests)
std::cout << "posix: GET_MEMORY_INFORMATION" << std::endl;

managarm::kerncfg::GetMemoryInformationRequest kerncfgRequest;
auto [kerncfgSendResp, kerncfgResp] = co_await helix_ng::exchangeMsgs(
getKerncfgLane(),
helix_ng::sendBragiHeadOnly(kerncfgRequest, frg::stl_allocator{}),
helix_ng::RecvInline{}
);
HEL_CHECK(kerncfgSendResp.error());
HEL_CHECK(kerncfgResp.error());

auto kernResp = bragi::parse_head_only<managarm::kerncfg::GetMemoryInformationResponse>(kerncfgResp);

managarm::posix::GetMemoryInformationResponse resp;
resp.set_total_usable_memory(kernResp->total_usable_memory());
resp.set_available_memory(kernResp->available_memory());
resp.set_memory_unit(kernResp->memory_unit());

auto [sendResp] = co_await helix_ng::exchangeMsgs(
conversation,
helix_ng::sendBragiHeadOnly(resp, frg::stl_allocator{})
);
HEL_CHECK(sendResp.error());
}else{
std::cout << "posix: Illegal request" << std::endl;
helix::SendBuffer send_resp;
Expand Down
2 changes: 2 additions & 0 deletions posix/subsystem/src/requests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@

async::result<void> serveRequests(std::shared_ptr<Process> self,
std::shared_ptr<Generation> generation);

helix::UniqueLane &getKerncfgLane();
12 changes: 12 additions & 0 deletions protocols/kerncfg/kerncfg.bragi
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ head(128):
tag(3) uint64 new_dequeue;
}
}

message GetMemoryInformationRequest 4 {
head(128):
}

message GetMemoryInformationResponse 5 {
head(128):
Error error;
uint64 total_usable_memory;
uint64 available_memory;
uint64 memory_unit;
}
11 changes: 11 additions & 0 deletions protocols/posix/posix.bragi
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,14 @@ head(128):
tail:
uint8[] mask;
}

message GetMemoryInformationRequest 91 {
head(128):
}

message GetMemoryInformationResponse 92 {
head(128):
uint64 total_usable_memory;
uint64 available_memory;
uint64 memory_unit;
}