diff options
author | Andreas Kling <kling@serenityos.org> | 2021-05-13 09:08:44 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-13 23:28:40 +0200 |
commit | e46343bf9aadaf8b310821c8a92b131f2b726654 (patch) | |
tree | 4d027e4b413e82b8482865c8a8c3d9b37cdaa0ca /Kernel | |
parent | 25a45e639ac48bc75507f003c692f24310688e9b (diff) | |
download | serenity-e46343bf9aadaf8b310821c8a92b131f2b726654.zip |
Kernel: Make UserOrKernelBuffer R/W helpers return KResultOr<size_t>
This makes error propagation less cumbersome (and also exposed some
places where we were not doing it.)
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/Console.cpp | 7 | ||||
-rw-r--r-- | Kernel/Devices/AsyncDeviceRequest.h | 4 | ||||
-rw-r--r-- | Kernel/Devices/HID/KeyboardDevice.cpp | 10 | ||||
-rw-r--r-- | Kernel/Devices/RandomDevice.cpp | 7 | ||||
-rw-r--r-- | Kernel/Devices/SerialDevice.cpp | 15 | ||||
-rw-r--r-- | Kernel/FileSystem/InodeWatcher.cpp | 8 | ||||
-rw-r--r-- | Kernel/FileSystem/ProcFS.cpp | 10 | ||||
-rw-r--r-- | Kernel/Process.h | 4 | ||||
-rw-r--r-- | Kernel/Storage/IDEChannel.cpp | 12 | ||||
-rw-r--r-- | Kernel/Syscalls/debug.cpp | 9 | ||||
-rw-r--r-- | Kernel/Syscalls/getrandom.cpp | 7 | ||||
-rw-r--r-- | Kernel/TTY/SlavePTY.cpp | 6 | ||||
-rw-r--r-- | Kernel/TTY/TTY.cpp | 24 | ||||
-rw-r--r-- | Kernel/TTY/VirtualConsole.cpp | 8 | ||||
-rw-r--r-- | Kernel/UserOrKernelBuffer.h | 18 |
15 files changed, 64 insertions, 85 deletions
diff --git a/Kernel/Console.cpp b/Kernel/Console.cpp index 340c4d3fa8..d91d838425 100644 --- a/Kernel/Console.cpp +++ b/Kernel/Console.cpp @@ -57,14 +57,11 @@ Kernel::KResultOr<size_t> Console::write(FileDescription&, u64, const Kernel::Us if (!size) return 0; - ssize_t nread = data.read_buffered<256>(size, [&](const u8* bytes, size_t bytes_count) { + return data.read_buffered<256>(size, [&](u8 const* bytes, size_t bytes_count) { for (size_t i = 0; i < bytes_count; i++) put_char((char)bytes[i]); - return (ssize_t)bytes_count; + return bytes_count; }); - if (nread < 0) - return Kernel::KResult((ErrnoCode)-nread); - return (size_t)nread; } void Console::put_char(char ch) diff --git a/Kernel/Devices/AsyncDeviceRequest.h b/Kernel/Devices/AsyncDeviceRequest.h index ab8ed3c626..b5c0035627 100644 --- a/Kernel/Devices/AsyncDeviceRequest.h +++ b/Kernel/Devices/AsyncDeviceRequest.h @@ -90,7 +90,7 @@ public: } template<size_t BUFFER_BYTES, typename... Args> - [[nodiscard]] bool write_to_buffer_buffered(UserOrKernelBuffer& buffer, Args... args) + [[nodiscard]] KResultOr<size_t> write_to_buffer_buffered(UserOrKernelBuffer& buffer, Args... args) { if (in_target_context(buffer)) return buffer.write_buffered<BUFFER_BYTES>(forward<Args>(args)...); @@ -108,7 +108,7 @@ public: } template<size_t BUFFER_BYTES, typename... Args> - [[nodiscard]] bool read_from_buffer_buffered(const UserOrKernelBuffer& buffer, Args... args) + [[nodiscard]] KResultOr<size_t> read_from_buffer_buffered(const UserOrKernelBuffer& buffer, Args... args) { if (in_target_context(buffer)) return buffer.read_buffered<BUFFER_BYTES>(forward<Args>(args)...); diff --git a/Kernel/Devices/HID/KeyboardDevice.cpp b/Kernel/Devices/HID/KeyboardDevice.cpp index f9e87e4ea7..b49603cbf9 100644 --- a/Kernel/Devices/HID/KeyboardDevice.cpp +++ b/Kernel/Devices/HID/KeyboardDevice.cpp @@ -298,13 +298,13 @@ KResultOr<size_t> KeyboardDevice::read(FileDescription&, u64, UserOrKernelBuffer lock.unlock(); - ssize_t n = buffer.write_buffered<sizeof(Event)>(sizeof(Event), [&](u8* data, size_t data_bytes) { + auto result = buffer.write_buffered<sizeof(Event)>(sizeof(Event), [&](u8* data, size_t data_bytes) { memcpy(data, &event, sizeof(Event)); - return (ssize_t)data_bytes; + return data_bytes; }); - if (n < 0) - return KResult((ErrnoCode)-n); - VERIFY((size_t)n == sizeof(Event)); + if (result.is_error()) + return result.error(); + VERIFY(result.value() == sizeof(Event)); nread += sizeof(Event); lock.lock(); diff --git a/Kernel/Devices/RandomDevice.cpp b/Kernel/Devices/RandomDevice.cpp index 589ed1eeb3..6ada0a36d4 100644 --- a/Kernel/Devices/RandomDevice.cpp +++ b/Kernel/Devices/RandomDevice.cpp @@ -25,13 +25,10 @@ bool RandomDevice::can_read(const FileDescription&, size_t) const KResultOr<size_t> RandomDevice::read(FileDescription&, u64, UserOrKernelBuffer& buffer, size_t size) { - bool success = buffer.write_buffered<256>(size, [&](u8* data, size_t data_size) { + return buffer.write_buffered<256>(size, [&](u8* data, size_t data_size) { get_good_random_bytes(data, data_size); - return (ssize_t)data_size; + return data_size; }); - if (!success) - return EFAULT; - return size; } KResultOr<size_t> RandomDevice::write(FileDescription&, u64, const UserOrKernelBuffer&, size_t size) diff --git a/Kernel/Devices/SerialDevice.cpp b/Kernel/Devices/SerialDevice.cpp index 263c684ffd..3be348172a 100644 --- a/Kernel/Devices/SerialDevice.cpp +++ b/Kernel/Devices/SerialDevice.cpp @@ -33,15 +33,11 @@ KResultOr<size_t> SerialDevice::read(FileDescription&, u64, UserOrKernelBuffer& if (!(get_line_status() & DataReady)) return 0; - ssize_t nwritten = buffer.write_buffered<128>(size, [&](u8* data, size_t data_size) { + return buffer.write_buffered<128>(size, [&](u8* data, size_t data_size) { for (size_t i = 0; i < data_size; i++) data[i] = IO::in8(m_base_addr); - return (ssize_t)data_size; + return data_size; }); - if (nwritten < 0) - return KResult((ErrnoCode)-nwritten); - - return size; } bool SerialDevice::can_write(const FileDescription&, size_t) const @@ -57,14 +53,11 @@ KResultOr<size_t> SerialDevice::write(FileDescription&, u64, const UserOrKernelB if (!(get_line_status() & EmptyTransmitterHoldingRegister)) return 0; - ssize_t nread = buffer.read_buffered<128>(size, [&](const u8* data, size_t data_size) { + return buffer.read_buffered<128>(size, [&](u8 const* data, size_t data_size) { for (size_t i = 0; i < data_size; i++) IO::out8(m_base_addr, data[i]); - return (ssize_t)data_size; + return data_size; }); - if (nread < 0) - return KResult((ErrnoCode)-nread); - return (size_t)nread; } String SerialDevice::device_name() const diff --git a/Kernel/FileSystem/InodeWatcher.cpp b/Kernel/FileSystem/InodeWatcher.cpp index fb7c36739a..29be55b502 100644 --- a/Kernel/FileSystem/InodeWatcher.cpp +++ b/Kernel/FileSystem/InodeWatcher.cpp @@ -48,7 +48,7 @@ KResultOr<size_t> InodeWatcher::read(FileDescription&, u64, UserOrKernelBuffer& if (buffer_size < bytes_to_write) return EINVAL; - ssize_t nwritten = buffer.write_buffered<MAXIMUM_EVENT_SIZE>(bytes_to_write, [&](u8* data, size_t data_bytes) { + auto result = buffer.write_buffered<MAXIMUM_EVENT_SIZE>(bytes_to_write, [&](u8* data, size_t data_bytes) { size_t offset = 0; memcpy(data + offset, &event.wd, sizeof(InodeWatcherEvent::watch_descriptor)); @@ -64,12 +64,10 @@ KResultOr<size_t> InodeWatcher::read(FileDescription&, u64, UserOrKernelBuffer& memset(data + offset, 0, sizeof(InodeWatcherEvent::name_length)); } - return (ssize_t)data_bytes; + return data_bytes; }); - if (nwritten < 0) - return KResult((ErrnoCode)-nwritten); evaluate_block_conditions(); - return bytes_to_write; + return result; } KResult InodeWatcher::close() diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp index 07789b904a..777bb5b936 100644 --- a/Kernel/FileSystem/ProcFS.cpp +++ b/Kernel/FileSystem/ProcFS.cpp @@ -903,17 +903,17 @@ static ssize_t write_sys_bool(InodeIdentifier inode_id, const UserOrKernelBuffer char value = 0; bool did_read = false; - ssize_t nread = buffer.read_buffered<1>(1, [&](const u8* data, size_t) { + auto result = buffer.read_buffered<1>(1, [&](u8 const* data, size_t) { if (did_read) return 0; value = (char)data[0]; did_read = true; return 1; }); - if (nread < 0) - return nread; - VERIFY(nread == 0 || (nread == 1 && did_read)); - if (nread == 0 || !(value == '0' || value == '1')) + if (result.is_error()) + return result.error(); + VERIFY(result.value() == 0 || (result.value() == 1 && did_read)); + if (result.value() == 0 || !(value == '0' || value == '1')) return (ssize_t)size; auto* lockable_bool = reinterpret_cast<Lockable<bool>*>(variable.address); diff --git a/Kernel/Process.h b/Kernel/Process.h index 94cbdccf27..7a8881e37e 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -255,7 +255,7 @@ public: KResultOr<int> sys$inode_watcher_add_watch(Userspace<const Syscall::SC_inode_watcher_add_watch_params*> user_params); KResultOr<int> sys$inode_watcher_remove_watch(int fd, int wd); KResultOr<int> sys$dbgputch(u8); - KResultOr<int> sys$dbgputstr(Userspace<const u8*>, int length); + KResultOr<size_t> sys$dbgputstr(Userspace<const u8*>, int length); KResultOr<int> sys$dump_backtrace(); KResultOr<pid_t> sys$gettid(); KResultOr<int> sys$donate(pid_t tid); @@ -372,7 +372,7 @@ public: KResultOr<int> sys$halt(); KResultOr<int> sys$reboot(); KResultOr<int> sys$realpath(Userspace<const Syscall::SC_realpath_params*>); - KResultOr<ssize_t> sys$getrandom(Userspace<void*>, size_t, unsigned int); + KResultOr<size_t> sys$getrandom(Userspace<void*>, size_t, unsigned int); KResultOr<int> sys$getkeymap(Userspace<const Syscall::SC_getkeymap_params*>); KResultOr<int> sys$setkeymap(Userspace<const Syscall::SC_setkeymap_params*>); KResultOr<int> sys$module_load(Userspace<const char*> path, size_t path_length); diff --git a/Kernel/Storage/IDEChannel.cpp b/Kernel/Storage/IDEChannel.cpp index 91403b26ae..5b5a54da33 100644 --- a/Kernel/Storage/IDEChannel.cpp +++ b/Kernel/Storage/IDEChannel.cpp @@ -476,12 +476,12 @@ bool IDEChannel::ata_do_read_sector() dbgln_if(PATA_DEBUG, "IDEChannel::ata_do_read_sector"); auto& request = *m_current_request; auto out_buffer = request.buffer().offset(m_current_request_block_index * 512); - ssize_t nwritten = request.write_to_buffer_buffered<512>(out_buffer, 512, [&](u8* buffer, size_t buffer_bytes) { + auto result = request.write_to_buffer_buffered<512>(out_buffer, 512, [&](u8* buffer, size_t buffer_bytes) { for (size_t i = 0; i < buffer_bytes; i += sizeof(u16)) *(u16*)&buffer[i] = IO::in16(m_io_group.io_base().offset(ATA_REG_DATA).get()); - return (ssize_t)buffer_bytes; + return buffer_bytes; }); - if (nwritten < 0) { + if (result.is_error()) { // TODO: Do we need to abort the PATA read if this wasn't the last block? complete_current_request(AsyncDeviceRequest::MemoryFault); return false; @@ -518,12 +518,12 @@ void IDEChannel::ata_do_write_sector() auto in_buffer = request.buffer().offset(m_current_request_block_index * 512); dbgln_if(PATA_DEBUG, "IDEChannel: Writing 512 bytes (part {}) (status={:#02x})...", m_current_request_block_index, status); - ssize_t nread = request.read_from_buffer_buffered<512>(in_buffer, 512, [&](const u8* buffer, size_t buffer_bytes) { + auto result = request.read_from_buffer_buffered<512>(in_buffer, 512, [&](u8 const* buffer, size_t buffer_bytes) { for (size_t i = 0; i < buffer_bytes; i += sizeof(u16)) IO::out16(m_io_group.io_base().offset(ATA_REG_DATA).get(), *(const u16*)&buffer[i]); - return (ssize_t)buffer_bytes; + return buffer_bytes; }); - if (nread < 0) + if (result.is_error()) complete_current_request(AsyncDeviceRequest::MemoryFault); } diff --git a/Kernel/Syscalls/debug.cpp b/Kernel/Syscalls/debug.cpp index d02bb185e9..5d2ddf24a6 100644 --- a/Kernel/Syscalls/debug.cpp +++ b/Kernel/Syscalls/debug.cpp @@ -23,7 +23,7 @@ KResultOr<int> Process::sys$dbgputch(u8 ch) return 0; } -KResultOr<int> Process::sys$dbgputstr(Userspace<const u8*> characters, int length) +KResultOr<size_t> Process::sys$dbgputstr(Userspace<const u8*> characters, int length) { if (length <= 0) return 0; @@ -31,14 +31,11 @@ KResultOr<int> Process::sys$dbgputstr(Userspace<const u8*> characters, int lengt auto buffer = UserOrKernelBuffer::for_user_buffer(characters, length); if (!buffer.has_value()) return EFAULT; - ssize_t nread = buffer.value().read_buffered<1024>(length, [&](const u8* buffer, size_t buffer_size) { + return buffer.value().read_buffered<1024>(length, [&](u8 const* buffer, size_t buffer_size) { for (size_t i = 0; i < buffer_size; ++i) IO::out8(0xe9, buffer[i]); - return (ssize_t)buffer_size; + return buffer_size; }); - if (nread < 0) - return (int)nread; - return 0; } } diff --git a/Kernel/Syscalls/getrandom.cpp b/Kernel/Syscalls/getrandom.cpp index a51c3aae35..8000b25ef2 100644 --- a/Kernel/Syscalls/getrandom.cpp +++ b/Kernel/Syscalls/getrandom.cpp @@ -13,7 +13,7 @@ namespace Kernel { // We don't use the flag yet, but we could use it for distinguishing // random source like Linux, unlike the OpenBSD equivalent. However, if we // do, we should be able of the caveats that Linux has dealt with. -KResultOr<ssize_t> Process::sys$getrandom(Userspace<void*> buffer, size_t buffer_size, [[maybe_unused]] unsigned flags) +KResultOr<size_t> Process::sys$getrandom(Userspace<void*> buffer, size_t buffer_size, [[maybe_unused]] unsigned flags) { REQUIRE_PROMISE(stdio); if (buffer_size <= 0) @@ -22,11 +22,10 @@ KResultOr<ssize_t> Process::sys$getrandom(Userspace<void*> buffer, size_t buffer auto data_buffer = UserOrKernelBuffer::for_user_buffer(buffer, buffer_size); if (!data_buffer.has_value()) return EFAULT; - ssize_t nwritten = data_buffer.value().write_buffered<1024>(buffer_size, [&](u8* buffer, size_t buffer_bytes) { + return data_buffer.value().write_buffered<1024>(buffer_size, [&](u8* buffer, size_t buffer_bytes) { get_good_random_bytes(buffer, buffer_bytes); - return (ssize_t)buffer_bytes; + return buffer_bytes; }); - return nwritten; } } diff --git a/Kernel/TTY/SlavePTY.cpp b/Kernel/TTY/SlavePTY.cpp index a53a378846..65bcd89a85 100644 --- a/Kernel/TTY/SlavePTY.cpp +++ b/Kernel/TTY/SlavePTY.cpp @@ -46,12 +46,12 @@ void SlavePTY::echo(u8 ch) void SlavePTY::on_master_write(const UserOrKernelBuffer& buffer, ssize_t size) { - ssize_t nread = buffer.read_buffered<128>(size, [&](const u8* data, size_t data_size) { + auto result = buffer.read_buffered<128>(size, [&](u8 const* data, size_t data_size) { for (size_t i = 0; i < data_size; ++i) emit(data[i], false); - return (ssize_t)data_size; + return data_size; }); - if (nread > 0) + if (!result.is_error()) evaluate_block_conditions(); } diff --git a/Kernel/TTY/TTY.cpp b/Kernel/TTY/TTY.cpp index 020150c86f..516b255e4d 100644 --- a/Kernel/TTY/TTY.cpp +++ b/Kernel/TTY/TTY.cpp @@ -43,10 +43,10 @@ KResultOr<size_t> TTY::read(FileDescription&, u64, UserOrKernelBuffer& buffer, s if (m_input_buffer.size() < static_cast<size_t>(size)) size = m_input_buffer.size(); - ssize_t nwritten; + KResultOr<size_t> result = 0; bool need_evaluate_block_conditions = false; if (in_canonical_mode()) { - nwritten = buffer.write_buffered<512>(size, [&](u8* data, size_t data_size) { + result = buffer.write_buffered<512>(size, [&](u8* data, size_t data_size) { size_t i = 0; for (; i < data_size; i++) { u8 ch = m_input_buffer.dequeue(); @@ -64,10 +64,10 @@ KResultOr<size_t> TTY::read(FileDescription&, u64, UserOrKernelBuffer& buffer, s } data[i] = ch; } - return (ssize_t)i; + return i; }); } else { - nwritten = buffer.write_buffered<512>(size, [&](u8* data, size_t data_size) { + result = buffer.write_buffered<512>(size, [&](u8* data, size_t data_size) { for (size_t i = 0; i < data_size; i++) { auto ch = m_input_buffer.dequeue(); if (ch == '\r' && m_termios.c_iflag & ICRNL) @@ -76,14 +76,12 @@ KResultOr<size_t> TTY::read(FileDescription&, u64, UserOrKernelBuffer& buffer, s ch = '\r'; data[i] = ch; } - return (ssize_t)data_size; + return data_size; }); } - if (nwritten < 0) - return KResult((ErrnoCode)-nwritten); - if (nwritten > 0 || need_evaluate_block_conditions) + if ((!result.is_error() && result.value() > 0) || need_evaluate_block_conditions) evaluate_block_conditions(); - return (size_t)nwritten; + return result; } KResultOr<size_t> TTY::write(FileDescription&, u64, const UserOrKernelBuffer& buffer, size_t size) @@ -93,8 +91,8 @@ KResultOr<size_t> TTY::write(FileDescription&, u64, const UserOrKernelBuffer& bu return EINTR; } - const size_t num_chars = 256; - ssize_t nread = buffer.read_buffered<num_chars>((size_t)size, [&](const u8* data, size_t buffer_bytes) { + constexpr size_t num_chars = 256; + return buffer.read_buffered<num_chars>(size, [&](u8 const* data, size_t buffer_bytes) { u8 modified_data[num_chars * 2]; size_t extra_chars = 0; for (size_t i = 0; i < buffer_bytes; ++i) { @@ -106,10 +104,8 @@ KResultOr<size_t> TTY::write(FileDescription&, u64, const UserOrKernelBuffer& bu modified_data[i + extra_chars] = ch; } on_tty_write(UserOrKernelBuffer::for_kernel_buffer(modified_data), buffer_bytes + extra_chars); - return (ssize_t)buffer_bytes; + return buffer_bytes; }); - - return nread; } bool TTY::can_read(const FileDescription&, size_t) const diff --git a/Kernel/TTY/VirtualConsole.cpp b/Kernel/TTY/VirtualConsole.cpp index e33fdbe7c0..5ac13edee4 100644 --- a/Kernel/TTY/VirtualConsole.cpp +++ b/Kernel/TTY/VirtualConsole.cpp @@ -226,14 +226,16 @@ void VirtualConsole::on_key_pressed(KeyEvent event) ssize_t VirtualConsole::on_tty_write(const UserOrKernelBuffer& data, ssize_t size) { ScopedSpinLock lock(s_lock); - ssize_t nread = data.read_buffered<512>((size_t)size, [&](const u8* buffer, size_t buffer_bytes) { + auto result = data.read_buffered<512>((size_t)size, [&](u8 const* buffer, size_t buffer_bytes) { for (size_t i = 0; i < buffer_bytes; ++i) m_terminal.on_input(buffer[i]); - return (ssize_t)buffer_bytes; + return buffer_bytes; }); if (m_active) flush_dirty_lines(); - return nread; + if (result.is_error()) + return result.error(); + return (ssize_t)result.value(); } void VirtualConsole::set_vga_start_row(u16 row) diff --git a/Kernel/UserOrKernelBuffer.h b/Kernel/UserOrKernelBuffer.h index 01fb1e2e8e..b5e79480ed 100644 --- a/Kernel/UserOrKernelBuffer.h +++ b/Kernel/UserOrKernelBuffer.h @@ -82,10 +82,10 @@ public: } template<size_t BUFFER_BYTES, typename F> - [[nodiscard]] ssize_t write_buffered(size_t offset, size_t len, F f) + [[nodiscard]] KResultOr<size_t> write_buffered(size_t offset, size_t len, F f) { if (!m_buffer) - return -EFAULT; + return EFAULT; if (is_kernel_buffer()) { // We're transferring directly to a kernel buffer, bypass return f(m_buffer + offset, len); @@ -102,24 +102,24 @@ public: return copied; VERIFY((size_t)copied <= to_copy); if (!write(buffer, nwritten, (size_t)copied)) - return -EFAULT; + return EFAULT; nwritten += (size_t)copied; if ((size_t)copied < to_copy) break; } - return (ssize_t)nwritten; + return nwritten; } template<size_t BUFFER_BYTES, typename F> - [[nodiscard]] ssize_t write_buffered(size_t len, F f) + [[nodiscard]] KResultOr<size_t> write_buffered(size_t len, F f) { return write_buffered<BUFFER_BYTES, F>(0, len, f); } template<size_t BUFFER_BYTES, typename F> - [[nodiscard]] ssize_t read_buffered(size_t offset, size_t len, F f) const + [[nodiscard]] KResultOr<size_t> read_buffered(size_t offset, size_t len, F f) const { if (!m_buffer) - return -EFAULT; + return EFAULT; if (is_kernel_buffer()) { // We're transferring directly from a kernel buffer, bypass return f(m_buffer + offset, len); @@ -132,7 +132,7 @@ public: while (nread < len) { auto to_copy = min(sizeof(buffer), len - nread); if (!read(buffer, nread, to_copy)) - return -EFAULT; + return EFAULT; ssize_t copied = f(buffer, to_copy); if (copied < 0) return copied; @@ -144,7 +144,7 @@ public: return nread; } template<size_t BUFFER_BYTES, typename F> - [[nodiscard]] ssize_t read_buffered(size_t len, F f) const + [[nodiscard]] KResultOr<size_t> read_buffered(size_t len, F f) const { return read_buffered<BUFFER_BYTES, F>(0, len, f); } |