Skip to content

Commit

Permalink
[WIP] sysdeps/managarm: move posix, fs error to c error conversion out
Browse files Browse the repository at this point in the history
This should reduce code size a bit, along with generally cleaning up
code.
  • Loading branch information
ElectrodeYT committed Dec 22, 2023
1 parent 3bccc2e commit 9a1e701
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 51 deletions.
80 changes: 33 additions & 47 deletions sysdeps/managarm/generic/drm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <mlibc/all-sysdeps.hpp>
#include <mlibc/allocator.hpp>
#include <mlibc/debug.hpp>
#include <mlibc/errors.hpp>
#include <mlibc/posix-pipe.hpp>

#include <fs.frigg_bragi.hpp>
Expand Down Expand Up @@ -35,7 +36,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

param->version_major = resp.drm_version_major();
param->version_minor = resp.drm_version_minor();
Expand Down Expand Up @@ -80,15 +81,11 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h
managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());

if(resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) {
return EINVAL;
}else{
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());;

param->value = resp.drm_value();
*result = resp.result();
return 0;
}
param->value = resp.drm_value();
*result = resp.result();
return 0;
}
case DRM_IOCTL_SET_CLIENT_CAP: {
auto param = reinterpret_cast<drm_set_client_cap *>(arg);
Expand All @@ -113,15 +110,11 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h
managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());

if(resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) {
return EINVAL;
}else{
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

param->value = resp.drm_value();
*result = resp.result();
return 0;
}
param->value = resp.drm_value();
*result = resp.result();
return 0;
}
case DRM_IOCTL_GET_MAGIC: {
auto param = reinterpret_cast<drm_auth *>(arg);
Expand Down Expand Up @@ -174,7 +167,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h
managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());

__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

for(size_t i = 0; i < resp.drm_fb_ids_size(); i++) {
if(i >= param->count_fbs)
Expand Down Expand Up @@ -245,7 +238,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

for(size_t i = 0; i < resp.drm_encoders_size(); i++) {
if(i >= param->count_encoders)
Expand Down Expand Up @@ -429,7 +422,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

param->crtc_id = resp.drm_crtc_id();
param->fb_id = resp.drm_fb_id();
Expand Down Expand Up @@ -473,7 +466,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

// FIXME: send this via a helix_ng buffer
for(size_t i = 0; i < resp.drm_plane_res_size(); i++) {
Expand Down Expand Up @@ -511,7 +504,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

param->encoder_type = resp.drm_encoder_type();
param->crtc_id = resp.drm_crtc_id();
Expand Down Expand Up @@ -546,7 +539,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

param->handle = resp.drm_handle();
param->pitch = resp.drm_pitch();
Expand Down Expand Up @@ -582,7 +575,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

param->fb_id = resp.drm_fb_id();

Expand Down Expand Up @@ -610,7 +603,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

param->width = resp.drm_width();
param->height = resp.drm_height();
Expand Down Expand Up @@ -659,7 +652,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

param->fb_id = resp.drm_fb_id();

Expand Down Expand Up @@ -688,7 +681,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

*result = resp.result();
return 0;
Expand All @@ -715,7 +708,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

param->offset = resp.drm_offset();

Expand Down Expand Up @@ -746,7 +739,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

param->fb_id = resp.drm_fb_id();
param->x = resp.drm_x();
Expand Down Expand Up @@ -790,7 +783,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

*result = resp.result();
return 0;
Expand Down Expand Up @@ -819,7 +812,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

auto props = reinterpret_cast<uint32_t *>(param->props_ptr);
auto prop_vals = reinterpret_cast<uint64_t *>(param->prop_values_ptr);
Expand Down Expand Up @@ -862,7 +855,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

*result = resp.result();
return 0;
Expand Down Expand Up @@ -901,13 +894,10 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h
managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());

if(resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) {
return EINVAL;
}else{
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
*result = resp.result();
return 0;
}
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

*result = resp.result();
return 0;
}
case DRM_IOCTL_MODE_CURSOR: {
auto param = reinterpret_cast<drm_mode_cursor *>(arg);
Expand Down Expand Up @@ -1003,7 +993,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

param->blob_id = resp.drm_blob_id();

Expand Down Expand Up @@ -1031,7 +1021,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

*result = resp.result();
return 0;
Expand Down Expand Up @@ -1131,7 +1121,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

param->fd = resp.drm_prime_fd();
*result = resp.result();
Expand Down Expand Up @@ -1160,11 +1150,7 @@ int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle h

managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
if(resp.error() == managarm::fs::Errors::FILE_NOT_FOUND) {
return EBADF;
} else {
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
}
FS_SUCCESS_OR_RETURN_ERROR(resp.error());

param->handle = resp.drm_prime_handle();
*result = resp.result();
Expand Down
1 change: 0 additions & 1 deletion sysdeps/managarm/generic/ensure.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#include <stdint.h>
#include <string.h>
#include <assert.h>
Expand Down
85 changes: 85 additions & 0 deletions sysdeps/managarm/generic/errors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <asm-generic/errno.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <errno.h>

#include <bits/ensure.h>
#include <mlibc/debug.hpp>
#include <mlibc/all-sysdeps.hpp>
#include <mlibc/errors.hpp>

#include <hel.h>
#include <hel-syscalls.h>

#include <posix.frigg_bragi.hpp>
#include <fs.frigg_bragi.hpp>

int posixErrorToError(managarm::posix::Errors posixError) {
using enum managarm::posix::Errors;

switch(posixError) {
case SUCCESS: return 0;
case DEAD_FORK: mlibc::panicLogger() << "mlibc: posix error DEAD_FORK must be handled manually" << frg::endlog; __builtin_unreachable();
case ILLEGAL_REQUEST: mlibc::panicLogger() << "mlibc: posix error ILLEGAL_REQUEST must be handled manually" << frg::endlog; __builtin_unreachable();
case ILLEGAL_ARGUMENTS: return EINVAL;
case FILE_NOT_FOUND: return ENOENT;
case ACCESS_DENIED: return EPERM;
case ALREADY_EXISTS: return EEXIST;
case NO_SUCH_FD: return EBADFD;
case END_OF_FILE: mlibc::panicLogger() << "mlibc: posix error END_OF_FILE must be handled manually" << frg::endlog; __builtin_unreachable();
case BAD_FD: return EBADFD;
case WOULD_BLOCK: return EWOULDBLOCK;
case BROKEN_PIPE: return EPIPE;
case NOT_SUPPORTED: return ENOTSUP;
case RESOURCE_IN_USE: return EBUSY;
case ILLEGAL_OPERATION_TARGET: return EINVAL;
case NOT_A_DIRECTORY: return ENOTDIR;
case NO_BACKING_DEVICE: return ENXIO;

// TODO: the name of this posix error implies that it might return other errors, since "ESRCH" is "No such process"
// Maybe we should rename this?
case NO_SUCH_RESOURCE: return ESRCH;
case INSUFFICIENT_PERMISSION: return EPERM;
case IS_DIRECTORY: return EISDIR;
case NOT_A_TTY: return ENOTTY;
case PROTOCOL_NOT_SUPPORTED: return EPROTONOSUPPORT;
case ADDRESS_FAMILY_NOT_SUPPORTED: return EAFNOSUPPORT;
case NO_MEMORY: return ENOMEM;
case INTERNAL_ERROR: return EIEIO;
default: mlibc::panicLogger() << "mlibc: invalid posix error: " << (int)posixError << frg::endlog; __builtin_unreachable();
}
}

int fsErrorToError(managarm::fs::Errors fsError) {
using enum managarm::fs::Errors;

switch(fsError) {
case SUCCESS: return 0;
case FILE_NOT_FOUND: return ENOENT;
case END_OF_FILE: mlibc::panicLogger() << "mlibc: posix error END_OF_FILE must be handled manually" << frg::endlog; __builtin_unreachable();
case ILLEGAL_ARGUMENT: return EINVAL;
case WOULD_BLOCK: return EWOULDBLOCK;
case SEEK_ON_PIPE: return ESPIPE;
case BROKEN_PIPE: return EPIPE;
case ACCESS_DENIED: return EPERM;
case AF_NOT_SUPPORTED: return EAFNOSUPPORT;
case DESTINATION_ADDRESS_REQUIRED: return EDESTADDRREQ;
case NETWORK_UNREACHABLE: return ENETUNREACH;
case MESSAGE_TOO_LARGE: return EMSGSIZE;
case HOST_UNREACHABLE: return EHOSTUNREACH;
case INSUFFICIENT_PERMISSIONS: return EPERM;
case ADDRESS_IN_USE: return EADDRINUSE;
case ADDRESS_NOT_AVAILABLE: return EADDRNOTAVAIL;
case NOT_CONNECTED: return ENOTCONN;
case ALREADY_EXISTS: return EEXIST;
case ILLEGAL_OPERATION_TARGET: return EINVAL;
case NOT_DIRECTORY: return ENOTDIR;
case NO_SPACE_LEFT: return ENOSPC;
case NOT_A_TERMINAL: return ENOTTY;
case NO_BACKING_DEVICE: return ENXIO;
case IS_DIRECTORY: return EISDIR;
case NOT_A_SOCKET: return ENOTSOCK;
default: mlibc::panicLogger() << "mlibc: invalid fs error: " << (int)posixError << frg::endlog; __builtin_unreachable();
}
}
9 changes: 6 additions & 3 deletions sysdeps/managarm/generic/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ int sys_sockname(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length,

managarm::fs::SvrResponse<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
if(resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) {
if(resp.error() == managarm::fs::Errors::NOT_A_SOCKET) {
return ENOTSOCK;
}
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
Expand Down Expand Up @@ -234,7 +234,7 @@ int sys_peername(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length,

managarm::fs::SvrResponse<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recvResp.data(), recvResp.length());
if(resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) {
if(resp.error() == managarm::fs::Errors::NOT_A_SOCKET) {
return ENOTSOCK;
}else if(resp.error() == managarm::fs::Errors::NOT_CONNECTED) {
return ENOTCONN;
Expand Down Expand Up @@ -364,11 +364,14 @@ int sys_setsockopt(int fd, int layer, int number,
}else if(layer == IPPROTO_TCP && number == TCP_KEEPIDLE) {
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with IPPROTO_TCP and TCP_KEEPIDLE is unimplemented\e[39m" << frg::endlog;
return 0;
}else if(layer == SOL_NETLINK && number == NETLINK_BROADCAST_ERROR) {
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_NETLINK and NETLINK_BROADCAST_ERROR is unimplemented\e[39m" << frg::endlog;
return 0;
}else if(layer == SOL_NETLINK && number == NETLINK_EXT_ACK) {
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_NETLINK and NETLINK_EXT_ACK is unimplemented\e[39m" << frg::endlog;
return 0;
}else if(layer == SOL_NETLINK && number == NETLINK_GET_STRICT_CHK) {
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_NETLINK and NETLINK_EXT_ACK is unimplemented\e[39m" << frg::endlog;
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_NETLINK and NETLINK_GET_STRICT_CHK is unimplemented\e[39m" << frg::endlog;
return 0;
}else if(layer == IPPROTO_TCP && number == TCP_KEEPINTVL) {
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with IPPROTO_TCP and TCP_KEEPINTVL is unimplemented\e[39m" << frg::endlog;
Expand Down
Loading

0 comments on commit 9a1e701

Please sign in to comment.