From 1cb8f1258dd7fd4fa8c65515e3ac3e1976d155bd Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Wed, 8 Feb 2023 22:44:05 -0800 Subject: [PATCH] [testapp] Re-add testapp to default manifest This commit re-adds testapp to the default manifest and does some housecleaning on the module: - Remove the old serial.* and io.* - Update it to use current syscall APIs - Update it to use libj6's higher-level thread API --- assets/manifests/default.yaml | 1 + src/libraries/j6/thread.cpp | 6 +++ src/user/testapp/io.cpp | 22 --------- src/user/testapp/io.h | 24 ---------- src/user/testapp/main.cpp | 82 +++++++++------------------------ src/user/testapp/serial.cpp | 41 ----------------- src/user/testapp/serial.h | 26 ----------- src/user/testapp/testapp.module | 2 - 8 files changed, 30 insertions(+), 174 deletions(-) delete mode 100644 src/user/testapp/io.cpp delete mode 100644 src/user/testapp/io.h delete mode 100644 src/user/testapp/serial.cpp delete mode 100644 src/user/testapp/serial.h diff --git a/assets/manifests/default.yaml b/assets/manifests/default.yaml index eec9e89..cdee1f1 100644 --- a/assets/manifests/default.yaml +++ b/assets/manifests/default.yaml @@ -8,5 +8,6 @@ panic: - panic.serial services: - srv.logger + - testapp drivers: - drv.uart diff --git a/src/libraries/j6/thread.cpp b/src/libraries/j6/thread.cpp index 0fd9cc3..4e4c770 100644 --- a/src/libraries/j6/thread.cpp +++ b/src/libraries/j6/thread.cpp @@ -45,6 +45,12 @@ thread::start() return m_status; } +void +thread::join() +{ + j6_thread_join(m_thread); +} + } #endif // __j6kernel diff --git a/src/user/testapp/io.cpp b/src/user/testapp/io.cpp deleted file mode 100644 index f5aa618..0000000 --- a/src/user/testapp/io.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "io.h" - -uint8_t -inb(uint16_t port) -{ - uint8_t val; - __asm__ __volatile__ ( "inb %1, %0" : "=a"(val) : "Nd"(port) ); - return val; -} - -void -outb(uint16_t port, uint8_t val) -{ - __asm__ __volatile__ ( "outb %0, %1" :: "a"(val), "Nd"(port) ); -} - -void -io_wait(unsigned times) -{ - for (unsigned i = 0; i < times; ++i) - outb(0x80, 0); -} diff --git a/src/user/testapp/io.h b/src/user/testapp/io.h deleted file mode 100644 index ebc7da0..0000000 --- a/src/user/testapp/io.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include - -extern "C" { - -/// Read a byte from an IO port. -/// \arg port The address of the IO port -/// \returns One byte read from the port -uint8_t inb(uint16_t port); - -/// Write a byte to an IO port. -/// \arg port The addres of the IO port -/// \arg val The byte to write -void outb(uint16_t port, uint8_t val); - -/// Pause briefly by doing IO to port 0x80 -/// \arg times Number of times to delay by writing -void io_wait(unsigned times = 1); - -} - -constexpr uint16_t COM1 = 0x03f8; -constexpr uint16_t COM2 = 0x02f8; diff --git a/src/user/testapp/main.cpp b/src/user/testapp/main.cpp index ce701bc..2fd9d22 100644 --- a/src/user/testapp/main.cpp +++ b/src/user/testapp/main.cpp @@ -1,13 +1,11 @@ #include #include -#include #include #include #include - -#include "io.h" -#include "serial.h" +#include +#include char inbuf[1024]; extern j6_handle_t __handle_sys; @@ -17,10 +15,10 @@ extern "C" { int main(int, const char **); } -constexpr j6_handle_t handle_self = 1; // Self program handle is always 1 +extern j6_handle_t __handle_self; -constexpr size_t stack_size = 0x10000; constexpr uintptr_t stack_top = 0xf80000000; +uint32_t flipflop = 0; void thread_proc() @@ -29,10 +27,13 @@ thread_proc() char buffer[512]; size_t len = sizeof(buffer); - j6_tag_t tag = 0; - j6_status_t result = j6_endpoint_receive(endp, &tag, (void*)buffer, &len); + j6_status_t result = j6_channel_receive(endp, (void*)buffer, &len, j6_channel_block); + + __sync_synchronize(); + flipflop = 1; + if (result != j6_status_ok) - j6_thread_exit(result); + j6_thread_exit(); j6_log("sub thread received message"); @@ -40,10 +41,9 @@ thread_proc() if (buffer[i] >= 'A' && buffer[i] <= 'Z') buffer[i] += 0x20; - tag++; - result = j6_endpoint_send(endp, tag, (void*)buffer, len); + result = j6_channel_send(endp, (void*)buffer, &len); if (result != j6_status_ok) - j6_thread_exit(result); + j6_thread_exit(); j6_log("sub thread sent message"); @@ -51,7 +51,7 @@ thread_proc() j6_thread_sleep(i*10); j6_log("sub thread exiting"); - j6_thread_exit(0); + j6_thread_exit(); } int @@ -74,72 +74,36 @@ main(int argc, const char **argv) j6_log("main thread wrote to memory area"); - j6_status_t result = j6_endpoint_create(&endp); + j6_status_t result = j6_channel_create(&endp); if (result != j6_status_ok) return result; - j6_log("main thread created endpoint"); + j6_log("main thread created channel"); - j6_handle_t child_stack_vma = j6_handle_invalid; - result = j6_vma_create_map(&child_stack_vma, stack_size, stack_top-stack_size, j6_vm_flag_write); - if (result != j6_status_ok) - return result; - - j6_handle_t child = j6_handle_invalid; - result = j6_thread_create(&child, handle_self, stack_top, reinterpret_cast(&thread_proc)); + j6::thread child_thread {thread_proc, stack_top}; + result = child_thread.start(); if (result != j6_status_ok) return result; j6_log("main thread created sub thread"); - char message[] = "MAIN THREAD SUCCESSFULLY CALLED SENDRECV IF THIS IS LOWERCASE"; + char message[] = "MAIN THREAD SUCCESSFULLY CALLED SEND AND RECEIVE IF THIS IS LOWERCASE"; size_t size = sizeof(message); - j6_tag_t tag = 16; - result = j6_endpoint_sendrecv(endp, &tag, (void*)message, &size); + + result = j6_channel_send(endp, (void*)message, &size); if (result != j6_status_ok) return result; - if (tag != 17) - j6_log("GOT WRONG TAG FROM SENDRECV"); + while (!flipflop); - result = j6_system_bind_irq(__handle_sys, endp, 3); + result = j6_channel_receive(endp, (void*)message, &size, j6_channel_block); if (result != j6_status_ok) return result; j6_log(message); j6_log("main thread waiting on sub thread"); - - result = j6_kobject_wait(child, -1ull, &out); - if (result != j6_status_ok) - return result; - - j6_log("main setting IOPL"); - - result = j6_system_request_iopl(__handle_sys, 3); - if (result != j6_status_ok) - return result; - - j6_log("main testing irqs"); - - serial_port com2(COM2); - - const char *fgseq = "\x1b[2J"; - while (*fgseq) - com2.write(*fgseq++); - - for (int i = 0; i < 10; ++i) - com2.write('%'); - - size_t len = 0; - while (true) { - result = j6_endpoint_receive(endp, &tag, nullptr, &len); - if (result != j6_status_ok) - return result; - - if (j6_tag_is_irq(tag)) - j6_log("main thread got irq!"); - } + child_thread.join(); j6_log("main thread done, exiting"); return 0; diff --git a/src/user/testapp/serial.cpp b/src/user/testapp/serial.cpp deleted file mode 100644 index f8de54b..0000000 --- a/src/user/testapp/serial.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "io.h" -#include "serial.h" - - -serial_port::serial_port() : - m_port(0) -{ -} - -serial_port::serial_port(uint16_t port) : - m_port(port) -{ - outb(port + 1, 0x00); // Disable all interrupts - outb(port + 3, 0x80); // Enable the Divisor Latch Access Bit - outb(port + 0, 0x01); // Divisor low bit: 1 (115200 baud) - outb(port + 1, 0x00); // Divisor high bit - outb(port + 3, 0x03); // 8-N-1 - outb(port + 2, 0xe7); // Clear and enable FIFO, enable 64 byte, 56 byte trigger - outb(port + 4, 0x0b); // Data terminal ready, Request to send, aux output 2 (irq enable) - outb(port + 1, 0x03); // Enable interrupts -} - -bool serial_port::read_ready() { return (inb(m_port + 5) & 0x01) != 0; } - -bool serial_port::write_ready() { - uint8_t lsr = inb(m_port + 5); - return (lsr & 0x20) != 0; -} - -char -serial_port::read() { - while (!read_ready()); - return inb(m_port); -} - -void -serial_port::write(char c) { - while (!write_ready()); - outb(m_port, c); -} - diff --git a/src/user/testapp/serial.h b/src/user/testapp/serial.h deleted file mode 100644 index 4bc7a16..0000000 --- a/src/user/testapp/serial.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once -/// \file serial.h -/// Declarations related to serial ports. -#include - -#define serial_port nulldrv_serial_port - -class serial_port -{ -public: - /// Constructor. - /// \arg port The IO address of the serial port - serial_port(uint16_t port); - - serial_port(); - - void write(char c); - char read(); - -private: - uint16_t m_port; - - bool read_ready(); - bool write_ready(); -}; - diff --git a/src/user/testapp/testapp.module b/src/user/testapp/testapp.module index 50b65e8..3a3a99a 100644 --- a/src/user/testapp/testapp.module +++ b/src/user/testapp/testapp.module @@ -5,7 +5,5 @@ module("testapp", deps = [ "libc" ], description = "Testbed app", sources = [ - "io.cpp", "main.cpp", - "serial.cpp", ])