Compare commits

..

5 Commits

Author SHA1 Message Date
71633e264f Kernel: Make the serial port writable for everyone :) 2022-11-03 17:13:32 +01:00
979569eb5a Ports: Refactor the NASM port's package script a bit 2022-11-03 17:06:27 +01:00
3b86f87c76 libc: Support %i in printf()
Which is the same as %d.
2022-11-03 16:53:19 +01:00
2cfefc25cc libc: Stub out rename() 2022-11-03 16:52:41 +01:00
0fd31698b2 Kernel: Accept not opened file descriptors in dup2()
This involves renaming the descriptor_from_fd function to the more appropriately named
open_descriptor_from_fd (since we check if the descriptor was opened and error out otherwise),
and creating a new function that does not verify that the file descriptor was opened.
2022-11-03 16:52:21 +01:00
9 changed files with 49 additions and 34 deletions

View File

@ -103,6 +103,7 @@ struct Task
bool is_still_blocking();
Descriptor* open_descriptor_from_fd(int fd, int& error);
Descriptor* descriptor_from_fd(int fd, int& error);
bool is_superuser();

View File

@ -16,7 +16,7 @@ VFS::Node* SerialDevice::create_new(const char* devname)
dev->type = VFS_DEVICE;
dev->flags = 0;
dev->uid = dev->gid = 0;
dev->mode = 0200;
dev->mode = 0222;
dev->atime = dev->ctime = dev->mtime = clock_boot();
strncpy(dev->name, devname, sizeof(dev->name));
return dev;

View File

@ -32,12 +32,8 @@ static int mman_flags_from_prot(int prot)
prot &= 0b111;
int flags = MAP_USER | MAP_AS_OWNED_BY_TASK;
if (prot == PROT_NONE) return MAP_AS_OWNED_BY_TASK;
if ((prot & PROT_WRITE) > 0) {
flags |= MAP_READ_WRITE;
}
if ((prot & PROT_EXEC) > 0) {
flags |= MAP_EXEC;
}
if ((prot & PROT_WRITE) > 0) { flags |= MAP_READ_WRITE; }
if ((prot & PROT_EXEC) > 0) { flags |= MAP_EXEC; }
return flags;
}
@ -72,11 +68,11 @@ void sys_mmap(Context* context, void* address, size_t size, int prot, int fd, of
return;
}
uint64_t addr_offset = (uint64_t)address % PAGE_SIZE;
if(fd >= 0)
if (fd >= 0)
{
int err;
Descriptor* file = Scheduler::current_task()->descriptor_from_fd(fd, err);
if(!file)
Descriptor* file = Scheduler::current_task()->open_descriptor_from_fd(fd, err);
if (!file)
{
context->rax = MAP_FAIL(err);
return;
@ -99,7 +95,8 @@ void sys_mmap(Context* context, void* address, size_t size, int prot, int fd, of
return;
}
}
kdbgln("mmap(): %ld pages at any address, %s, fd %d", Utilities::get_blocks_from_size(PAGE_SIZE, size), format_prot(prot), fd);
kdbgln("mmap(): %ld pages at any address, %s, fd %d", Utilities::get_blocks_from_size(PAGE_SIZE, size),
format_prot(prot), fd);
uint64_t ptr =
Scheduler::current_task()->allocator.request_virtual_pages(Utilities::get_blocks_from_size(PAGE_SIZE, size));
if (!ptr)
@ -108,11 +105,11 @@ void sys_mmap(Context* context, void* address, size_t size, int prot, int fd, of
context->rax = MAP_FAIL(ENOMEM);
return;
}
if(fd >= 0)
if (fd >= 0)
{
int err;
Descriptor* file = Scheduler::current_task()->descriptor_from_fd(fd, err);
if(!file)
Descriptor* file = Scheduler::current_task()->open_descriptor_from_fd(fd, err);
if (!file)
{
context->rax = MAP_FAIL(err);
return;
@ -172,10 +169,12 @@ void sys_munmap(Context* context, void* address, size_t size)
uint64_t offset = (uint64_t)address % PAGE_SIZE;
Scheduler::current_task()->allocator.free_virtual_pages(((uint64_t)address - offset),
Utilities::get_blocks_from_size(PAGE_SIZE, size));
if(flags & MAP_AS_OWNED_BY_TASK)
MemoryManager::release_pages((void*)((uint64_t)address - offset), Utilities::get_blocks_from_size(PAGE_SIZE, size));
if (flags & MAP_AS_OWNED_BY_TASK)
MemoryManager::release_pages((void*)((uint64_t)address - offset),
Utilities::get_blocks_from_size(PAGE_SIZE, size));
else
MemoryManager::release_unaligned_mappings((void*)((uint64_t)address - offset), Utilities::get_blocks_from_size(PAGE_SIZE, size));
MemoryManager::release_unaligned_mappings((void*)((uint64_t)address - offset),
Utilities::get_blocks_from_size(PAGE_SIZE, size));
kdbgln("munmap() succeeded");
context->rax = 0;
return;
@ -219,7 +218,8 @@ void sys_mprotect(Context* context, void* address, size_t size, int prot)
uint64_t offset = (uint64_t)address % PAGE_SIZE;
MemoryManager::protect((void*)((uint64_t)address - offset), Utilities::get_blocks_from_size(PAGE_SIZE, size),
flags & MAP_AS_OWNED_BY_TASK ? mman_flags_from_prot(prot) : mman_flags_from_prot(prot) & ~(MAP_AS_OWNED_BY_TASK));
flags & MAP_AS_OWNED_BY_TASK ? mman_flags_from_prot(prot)
: mman_flags_from_prot(prot) & ~(MAP_AS_OWNED_BY_TASK));
kdbgln("mprotect() succeeded");
context->rax = 0;
return;

View File

@ -38,7 +38,7 @@ void sys_fcntl(Context* context, int fd, int command, uintptr_t arg)
{
Task* current_task = Scheduler::current_task();
int err;
Descriptor* file = current_task->descriptor_from_fd(fd, err);
Descriptor* file = current_task->open_descriptor_from_fd(fd, err);
if (!file)
{
context->rax = -err;
@ -102,7 +102,7 @@ void sys_seek(Context* context, int fd, long offset, int whence)
return;
}
int err;
Descriptor* file = Scheduler::current_task()->descriptor_from_fd(fd, err);
Descriptor* file = Scheduler::current_task()->open_descriptor_from_fd(fd, err);
if (!file)
{
context->rax = -err;
@ -144,7 +144,7 @@ void sys_write(Context* context, int fd, size_t size, const char* addr)
return;
}
int err;
Descriptor* file = Scheduler::current_task()->descriptor_from_fd(fd, err);
Descriptor* file = Scheduler::current_task()->open_descriptor_from_fd(fd, err);
if (!file)
{
context->rax = -err;
@ -155,7 +155,8 @@ void sys_write(Context* context, int fd, size_t size, const char* addr)
context->rax = -EBADF;
return;
}
ssize_t result = file->write(size, (const char*)VMM::get_physical((uint64_t)addr));
ssize_t result = file->write(
size, (const char*)VMM::get_physical((uint64_t)addr)); // FIXME: Handle big buffers and invalid addresses.
context->rax = (size_t)result;
return;
}
@ -274,13 +275,13 @@ void sys_read(Context* context, int fd, size_t size, char* buffer)
return;
}
int err;
Descriptor* file = Scheduler::current_task()->descriptor_from_fd(fd, err);
Descriptor* file = Scheduler::current_task()->open_descriptor_from_fd(fd, err);
if (!file)
{
context->rax = -err;
return;
}
if (!file->is_open() || !file->can_read())
if (!file->can_read())
{
context->rax = -EBADF;
return;
@ -310,7 +311,7 @@ void sys_read(Context* context, int fd, size_t size, char* buffer)
void sys_close(Context* context, int fd)
{
int err;
Descriptor* file = Scheduler::current_task()->descriptor_from_fd(fd, err);
Descriptor* file = Scheduler::current_task()->open_descriptor_from_fd(fd, err);
if (!file)
{
context->rax = -err;
@ -352,13 +353,13 @@ void sys_access(Context* context, const char* path, int) // FIXME: Use the amode
void sys_dup2(Context* context, int fd, int fd2)
{
int err;
Descriptor* file1 = Scheduler::current_task()->descriptor_from_fd(fd, err);
Descriptor* file1 = Scheduler::current_task()->open_descriptor_from_fd(fd, err);
if (!file1)
{
context->rax = -err;
return;
}
Descriptor* file2 = Scheduler::current_task()->descriptor_from_fd(fd2, err); // FIXME: This will return EBADF if fd2 is not open, but we want to replace it with fd1 if it is not open.
Descriptor* file2 = Scheduler::current_task()->descriptor_from_fd(fd2, err);
if (!file2)
{
context->rax = -err;
@ -380,7 +381,7 @@ void sys_umask(Context* context, mode_t cmask)
void sys_ioctl(Context* context, int fd, int cmd, uintptr_t arg)
{
int err;
Descriptor* file = Scheduler::current_task()->descriptor_from_fd(fd, err);
Descriptor* file = Scheduler::current_task()->open_descriptor_from_fd(fd, err);
if (!file)
{
context->rax = -err;

View File

@ -112,14 +112,21 @@ bool Task::is_still_blocking()
}
}
Descriptor* Task::descriptor_from_fd(int fd, int& error)
Descriptor* Task::open_descriptor_from_fd(int fd, int& error)
{
if (fd < 0 || fd >= TASK_MAX_FDS)
Descriptor* file = descriptor_from_fd(fd, error);
if (!file) return nullptr;
if (!file->is_open())
{
error = EBADF;
return nullptr;
}
if (!files[fd].is_open())
return file;
}
Descriptor* Task::descriptor_from_fd(int fd, int& error)
{
if (fd < 0 || fd >= TASK_MAX_FDS)
{
error = EBADF;
return nullptr;

View File

@ -163,7 +163,8 @@ extern "C"
* of the last error encountered during a call to a system or library function. */
void perror(const char* str);
int remove(const char* pathname); // Not implemented.
int remove(const char* pathname); // Not implemented.
int rename(const char* oldpath, const char* newpath); // Not implemented.
FILE* tmpfile(void); // Not implemented.

View File

@ -185,6 +185,7 @@ static int internal_printf(const char* format, PutString put_string_callback, ss
preserve_format = true;
break;
}
case 'i':
case 'd': {
char result[32];
if (is_unsigned_long)

View File

@ -64,6 +64,11 @@ extern "C"
NOT_IMPLEMENTED("fscanf");
}
int rename(const char*, const char*)
{
NOT_IMPLEMENTED("rename");
}
FILE* tmpfile()
{
errno = ENOTSUP;

View File

@ -37,6 +37,5 @@ port_install()
port_uninstall() # nasm's Makefile does not provide an uninstall target.
{
rm -f $DESTDIR/bin/nasm
rm -f $DESTDIR/bin/ndisasm
rm -f $DESTDIR/bin/{nasm,ndisasm}
}