libc: Implement mmap() via a VM_MAP posix request
@@ -657,22 +657,16 @@ char *ttyname(int fd) {
}
void *mmap(void *hint, size_t size, int prot, int flags, int fd, off_t offset) {
- // TODO: Raise EINVAL instead of __ensure()ing.
- auto valid_prot = PROT_READ | PROT_WRITE;
- auto valid_flags = MAP_SHARED;
- __ensure(!(prot & ~valid_prot));
- __ensure(!(flags & ~valid_flags));
+ __ensure(!hint);
- __ensure(flags & MAP_SHARED);
-
- HelAction actions[4];
+ HelAction actions[3];
globalQueue.trim();
- auto handle = cacheFileTable()[fd];
- __ensure(handle);
-
- managarm::fs::CntRequest<MemoryAllocator> req(getAllocator());
- req.set_req_type(managarm::fs::CntReqType::MMAP);
+ managarm::posix::CntRequest<MemoryAllocator> req(getAllocator());
+ req.set_request_type(managarm::posix::CntReqType::VM_MAP);
+ req.set_size(size);
+ req.set_mode(prot);
+ req.set_flags(flags);
req.set_fd(fd);
req.set_rel_offset(offset);
@@ -685,45 +679,23 @@ void *mmap(void *hint, size_t size, int prot, int flags, int fd, off_t offset) {
actions[1].buffer = ser.data();
actions[1].length = ser.size();
actions[2].type = kHelActionRecvInline;
- actions[2].flags = kHelItemChain;
- actions[3].type = kHelActionPullDescriptor;
- actions[3].flags = 0;
- HEL_CHECK(helSubmitAsync(handle, actions, 4,
+ actions[2].flags = 0;
+ HEL_CHECK(helSubmitAsync(kHelThisThread, actions, 3,
globalQueue.getQueue(), 0, 0));
auto element = globalQueue.dequeueSingle();
auto offer = parseSimple(element);
auto send_req = parseSimple(element);
auto recv_resp = parseInline(element);
- auto pull_memory = parseHandle(element);
HEL_CHECK(offer->error);
HEL_CHECK(send_req->error);
HEL_CHECK(recv_resp->error);
- HEL_CHECK(pull_memory->error);
- managarm::fs::SvrResponse<MemoryAllocator> resp(getAllocator());
+ managarm::posix::SvrResponse<MemoryAllocator> resp(getAllocator());
resp.ParseFromArray(recv_resp->data, recv_resp->length);
- __ensure(resp.error() == managarm::fs::Errors::SUCCESS);
-
- uint32_t native_flags = kHelMapShareAtFork;
-
- // TODO: Raise EINVAL instead of __ensure()ing.
- if(prot & PROT_EXEC) {
- native_flags |= kHelMapReadExecute;
- }else if(prot & PROT_WRITE) {
- native_flags |= kHelMapReadWrite;
- }else if(prot & PROT_READ) {
- native_flags |= kHelMapReadOnly;
- }else{
- __ensure("mmap(): Protection not supported");
- }
-
- void *pointer;
- HEL_CHECK(helMapMemory(pull_memory->handle, kHelNullHandle,
- nullptr, resp.offset(), size, native_flags, &pointer));
- HEL_CHECK(helCloseDescriptor(pull_memory->handle));
- return pointer;
+ __ensure(resp.error() == managarm::posix::Errors::SUCCESS);
+ return reinterpret_cast<void *>(resp.offset());
}
int munmap(void *pointer, size_t size) {
