diff options
Diffstat (limited to 'src')
35 files changed, 1292 insertions, 30 deletions
diff --git a/src/Makefile b/src/Makefile index aacde54..b0b6a58 100644 --- a/src/Makefile +++ b/src/Makefile @@ -43,9 +43,11 @@ root: mkdir -p $(SYSROOT)/boot/np/ mkdir -p $(SYSROOT)/usr/include/ mkdir -p $(SYSROOT)/usr/bin/ + mkdir -p $(SYSROOT)/usr/sbin/ mkdir -p sys/target/header/ rsync -av sys/include/arch/$(TARGET)/ sys/target/header/machine/ rsync -av sys/np/sample/*.np $(SYSROOT)/boot/np/ + rsync -avr data/ucred/ root/ucred .PHONY: sysroot image: diff --git a/src/cmd/Makefile b/src/cmd/Makefile index f0057af..e4ff13e 100644 --- a/src/cmd/Makefile +++ b/src/cmd/Makefile @@ -16,6 +16,8 @@ all: LIBC_DIR=$(shell pwd)/../$(LIBC_DIR) cd lsdisk/; make LDSCRIPT=$(LDSCRIPT) CC=$(CC) AS=$(AS) LD=$(LD) SYSROOT=$(SYSROOT) \ LIBC_DIR=$(shell pwd)/../$(LIBC_DIR) + cd login/; make LDSCRIPT=$(LDSCRIPT) CC=$(CC) AS=$(AS) LD=$(LD) SYSROOT=$(SYSROOT) \ + LIBC_DIR=$(shell pwd)/../$(LIBC_DIR) .PHONY: clean clean: @@ -24,3 +26,4 @@ clean: cd reboot/; make clean cd fetch/; make clean cd lsdisk/; make clean + cd login/; make clean diff --git a/src/cmd/init/init.c b/src/cmd/init/init.c index 6b05bfa..3f39743 100644 --- a/src/cmd/init/init.c +++ b/src/cmd/init/init.c @@ -33,9 +33,9 @@ void main(void) { - char shell_path[] = "/usr/bin/hush"; - char *argv_dmmy[] = { "/usr/bin/hush", NULL }; + char login_path[] = "/usr/sbin/login"; + char *argv_dmmy[] = { "/usr/sbin/login", NULL }; - spawn(shell_path, argv_dmmy); + spawn(login_path, argv_dmmy); for (;;); } diff --git a/src/cmd/login/Makefile b/src/cmd/login/Makefile new file mode 100644 index 0000000..f0018e8 --- /dev/null +++ b/src/cmd/login/Makefile @@ -0,0 +1,18 @@ +include ../../data/build/user.mk + +CFILES = $(shell find . -name "*.c") +CFILES = $(shell find . -name "*.c") +CFLAGS = -L$(LIBC_DIR) -lc $(INTERNAL_CFLAGS) \ + -L../../lib/libwidget -lwidget -L../../lib/libc/ -lc \ + -I../../lib/libwidget/include/ +OBJECTS = $(CFILES:%.c=%.o) + +$(SYSROOT)/usr/sbin/login: $(OBJECTS) + $(LD) $(OBJECTS) -o $@ $(CFLAGS) + +%.o: %.c + $(CC) $(INTERNAL_CFLAGS) -c $(CFLAGS) $< -o $@ + +.PHONY: clean +clean: + rm -f *.o *.d diff --git a/src/cmd/login/blake2-impl.h b/src/cmd/login/blake2-impl.h new file mode 100644 index 0000000..c1df82e --- /dev/null +++ b/src/cmd/login/blake2-impl.h @@ -0,0 +1,160 @@ +/* + BLAKE2 reference source code package - reference C implementations + + Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the + terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at + your option. The terms of these licenses can be found at: + + - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 + - OpenSSL license : https://www.openssl.org/source/license.html + - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0 + + More information about the BLAKE2 hash function can be found at + https://blake2.net. +*/ +#ifndef BLAKE2_IMPL_H +#define BLAKE2_IMPL_H + +#include <stdint.h> +#include <string.h> + +#if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L) + #if defined(_MSC_VER) + #define BLAKE2_INLINE __inline + #elif defined(__GNUC__) + #define BLAKE2_INLINE __inline__ + #else + #define BLAKE2_INLINE + #endif +#else + #define BLAKE2_INLINE inline +#endif + +static BLAKE2_INLINE uint32_t load32( const void *src ) +{ +#if defined(NATIVE_LITTLE_ENDIAN) + uint32_t w; + memcpy(&w, src, sizeof w); + return w; +#else + const uint8_t *p = ( const uint8_t * )src; + return (( uint32_t )( p[0] ) << 0) | + (( uint32_t )( p[1] ) << 8) | + (( uint32_t )( p[2] ) << 16) | + (( uint32_t )( p[3] ) << 24) ; +#endif +} + +static BLAKE2_INLINE uint64_t load64( const void *src ) +{ +#if defined(NATIVE_LITTLE_ENDIAN) + uint64_t w; + memcpy(&w, src, sizeof w); + return w; +#else + const uint8_t *p = ( const uint8_t * )src; + return (( uint64_t )( p[0] ) << 0) | + (( uint64_t )( p[1] ) << 8) | + (( uint64_t )( p[2] ) << 16) | + (( uint64_t )( p[3] ) << 24) | + (( uint64_t )( p[4] ) << 32) | + (( uint64_t )( p[5] ) << 40) | + (( uint64_t )( p[6] ) << 48) | + (( uint64_t )( p[7] ) << 56) ; +#endif +} + +static BLAKE2_INLINE uint16_t load16( const void *src ) +{ +#if defined(NATIVE_LITTLE_ENDIAN) + uint16_t w; + memcpy(&w, src, sizeof w); + return w; +#else + const uint8_t *p = ( const uint8_t * )src; + return ( uint16_t )((( uint32_t )( p[0] ) << 0) | + (( uint32_t )( p[1] ) << 8)); +#endif +} + +static BLAKE2_INLINE void store16( void *dst, uint16_t w ) +{ +#if defined(NATIVE_LITTLE_ENDIAN) + memcpy(dst, &w, sizeof w); +#else + uint8_t *p = ( uint8_t * )dst; + *p++ = ( uint8_t )w; w >>= 8; + *p++ = ( uint8_t )w; +#endif +} + +static BLAKE2_INLINE void store32( void *dst, uint32_t w ) +{ +#if defined(NATIVE_LITTLE_ENDIAN) + memcpy(dst, &w, sizeof w); +#else + uint8_t *p = ( uint8_t * )dst; + p[0] = (uint8_t)(w >> 0); + p[1] = (uint8_t)(w >> 8); + p[2] = (uint8_t)(w >> 16); + p[3] = (uint8_t)(w >> 24); +#endif +} + +static BLAKE2_INLINE void store64( void *dst, uint64_t w ) +{ +#if defined(NATIVE_LITTLE_ENDIAN) + memcpy(dst, &w, sizeof w); +#else + uint8_t *p = ( uint8_t * )dst; + p[0] = (uint8_t)(w >> 0); + p[1] = (uint8_t)(w >> 8); + p[2] = (uint8_t)(w >> 16); + p[3] = (uint8_t)(w >> 24); + p[4] = (uint8_t)(w >> 32); + p[5] = (uint8_t)(w >> 40); + p[6] = (uint8_t)(w >> 48); + p[7] = (uint8_t)(w >> 56); +#endif +} + +static BLAKE2_INLINE uint64_t load48( const void *src ) +{ + const uint8_t *p = ( const uint8_t * )src; + return (( uint64_t )( p[0] ) << 0) | + (( uint64_t )( p[1] ) << 8) | + (( uint64_t )( p[2] ) << 16) | + (( uint64_t )( p[3] ) << 24) | + (( uint64_t )( p[4] ) << 32) | + (( uint64_t )( p[5] ) << 40) ; +} + +static BLAKE2_INLINE void store48( void *dst, uint64_t w ) +{ + uint8_t *p = ( uint8_t * )dst; + p[0] = (uint8_t)(w >> 0); + p[1] = (uint8_t)(w >> 8); + p[2] = (uint8_t)(w >> 16); + p[3] = (uint8_t)(w >> 24); + p[4] = (uint8_t)(w >> 32); + p[5] = (uint8_t)(w >> 40); +} + +static BLAKE2_INLINE uint32_t rotr32( const uint32_t w, const unsigned c ) +{ + return ( w >> c ) | ( w << ( 32 - c ) ); +} + +static BLAKE2_INLINE uint64_t rotr64( const uint64_t w, const unsigned c ) +{ + return ( w >> c ) | ( w << ( 64 - c ) ); +} + +/* prevents compiler optimizing out memset() */ +static BLAKE2_INLINE void secure_zero_memory(void *v, size_t n) +{ + static void *(*const volatile memset_v)(void *, int, size_t) = &memset; + memset_v(v, 0, n); +} + +#endif diff --git a/src/cmd/login/blake2.h b/src/cmd/login/blake2.h new file mode 100644 index 0000000..ca39030 --- /dev/null +++ b/src/cmd/login/blake2.h @@ -0,0 +1,195 @@ +/* + BLAKE2 reference source code package - reference C implementations + + Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the + terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at + your option. The terms of these licenses can be found at: + + - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 + - OpenSSL license : https://www.openssl.org/source/license.html + - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0 + + More information about the BLAKE2 hash function can be found at + https://blake2.net. +*/ +#ifndef BLAKE2_H +#define BLAKE2_H + +#include <stddef.h> +#include <stdint.h> + +#if defined(_MSC_VER) +#define BLAKE2_PACKED(x) __pragma(pack(push, 1)) x __pragma(pack(pop)) +#else +#define BLAKE2_PACKED(x) x __attribute__((packed)) +#endif + +#if defined(__cplusplus) +extern "C" { +#endif + + enum blake2s_constant + { + BLAKE2S_BLOCKBYTES = 64, + BLAKE2S_OUTBYTES = 32, + BLAKE2S_KEYBYTES = 32, + BLAKE2S_SALTBYTES = 8, + BLAKE2S_PERSONALBYTES = 8 + }; + + enum blake2b_constant + { + BLAKE2B_BLOCKBYTES = 128, + BLAKE2B_OUTBYTES = 64, + BLAKE2B_KEYBYTES = 64, + BLAKE2B_SALTBYTES = 16, + BLAKE2B_PERSONALBYTES = 16 + }; + + typedef struct blake2s_state__ + { + uint32_t h[8]; + uint32_t t[2]; + uint32_t f[2]; + uint8_t buf[BLAKE2S_BLOCKBYTES]; + size_t buflen; + size_t outlen; + uint8_t last_node; + } blake2s_state; + + typedef struct blake2b_state__ + { + uint64_t h[8]; + uint64_t t[2]; + uint64_t f[2]; + uint8_t buf[BLAKE2B_BLOCKBYTES]; + size_t buflen; + size_t outlen; + uint8_t last_node; + } blake2b_state; + + typedef struct blake2sp_state__ + { + blake2s_state S[8][1]; + blake2s_state R[1]; + uint8_t buf[8 * BLAKE2S_BLOCKBYTES]; + size_t buflen; + size_t outlen; + } blake2sp_state; + + typedef struct blake2bp_state__ + { + blake2b_state S[4][1]; + blake2b_state R[1]; + uint8_t buf[4 * BLAKE2B_BLOCKBYTES]; + size_t buflen; + size_t outlen; + } blake2bp_state; + + + BLAKE2_PACKED(struct blake2s_param__ + { + uint8_t digest_length; /* 1 */ + uint8_t key_length; /* 2 */ + uint8_t fanout; /* 3 */ + uint8_t depth; /* 4 */ + uint32_t leaf_length; /* 8 */ + uint32_t node_offset; /* 12 */ + uint16_t xof_length; /* 14 */ + uint8_t node_depth; /* 15 */ + uint8_t inner_length; /* 16 */ + /* uint8_t reserved[0]; */ + uint8_t salt[BLAKE2S_SALTBYTES]; /* 24 */ + uint8_t personal[BLAKE2S_PERSONALBYTES]; /* 32 */ + }); + + typedef struct blake2s_param__ blake2s_param; + + BLAKE2_PACKED(struct blake2b_param__ + { + uint8_t digest_length; /* 1 */ + uint8_t key_length; /* 2 */ + uint8_t fanout; /* 3 */ + uint8_t depth; /* 4 */ + uint32_t leaf_length; /* 8 */ + uint32_t node_offset; /* 12 */ + uint32_t xof_length; /* 16 */ + uint8_t node_depth; /* 17 */ + uint8_t inner_length; /* 18 */ + uint8_t reserved[14]; /* 32 */ + uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */ + uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */ + }); + + typedef struct blake2b_param__ blake2b_param; + + typedef struct blake2xs_state__ + { + blake2s_state S[1]; + blake2s_param P[1]; + } blake2xs_state; + + typedef struct blake2xb_state__ + { + blake2b_state S[1]; + blake2b_param P[1]; + } blake2xb_state; + + /* Padded structs result in a compile-time error */ + enum { + BLAKE2_DUMMY_1 = 1/(int)(sizeof(blake2s_param) == BLAKE2S_OUTBYTES), + BLAKE2_DUMMY_2 = 1/(int)(sizeof(blake2b_param) == BLAKE2B_OUTBYTES) + }; + + /* Streaming API */ + int blake2s_init( blake2s_state *S, size_t outlen ); + int blake2s_init_key( blake2s_state *S, size_t outlen, const void *key, size_t keylen ); + int blake2s_init_param( blake2s_state *S, const blake2s_param *P ); + int blake2s_update( blake2s_state *S, const void *in, size_t inlen ); + int blake2s_final( blake2s_state *S, void *out, size_t outlen ); + + int blake2b_init( blake2b_state *S, size_t outlen ); + int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen ); + int blake2b_init_param( blake2b_state *S, const blake2b_param *P ); + int blake2b_update( blake2b_state *S, const void *in, size_t inlen ); + int blake2b_final( blake2b_state *S, void *out, size_t outlen ); + + int blake2sp_init( blake2sp_state *S, size_t outlen ); + int blake2sp_init_key( blake2sp_state *S, size_t outlen, const void *key, size_t keylen ); + int blake2sp_update( blake2sp_state *S, const void *in, size_t inlen ); + int blake2sp_final( blake2sp_state *S, void *out, size_t outlen ); + + int blake2bp_init( blake2bp_state *S, size_t outlen ); + int blake2bp_init_key( blake2bp_state *S, size_t outlen, const void *key, size_t keylen ); + int blake2bp_update( blake2bp_state *S, const void *in, size_t inlen ); + int blake2bp_final( blake2bp_state *S, void *out, size_t outlen ); + + /* Variable output length API */ + int blake2xs_init( blake2xs_state *S, const size_t outlen ); + int blake2xs_init_key( blake2xs_state *S, const size_t outlen, const void *key, size_t keylen ); + int blake2xs_update( blake2xs_state *S, const void *in, size_t inlen ); + int blake2xs_final(blake2xs_state *S, void *out, size_t outlen); + + int blake2xb_init( blake2xb_state *S, const size_t outlen ); + int blake2xb_init_key( blake2xb_state *S, const size_t outlen, const void *key, size_t keylen ); + int blake2xb_update( blake2xb_state *S, const void *in, size_t inlen ); + int blake2xb_final(blake2xb_state *S, void *out, size_t outlen); + + /* Simple API */ + int blake2s( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); + int blake2b( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); + + int blake2sp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); + int blake2bp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); + + int blake2xs( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); + int blake2xb( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); + + /* This is simply an alias for blake2b */ + int blake2( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/src/cmd/login/blake2b.c b/src/cmd/login/blake2b.c new file mode 100644 index 0000000..752f8e3 --- /dev/null +++ b/src/cmd/login/blake2b.c @@ -0,0 +1,311 @@ +/* + BLAKE2 reference source code package - reference C implementations + + Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the + terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at + your option. The terms of these licenses can be found at: + + - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 + - OpenSSL license : https://www.openssl.org/source/license.html + - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0 + + More information about the BLAKE2 hash function can be found at + https://blake2.net. +*/ + +#include <stdint.h> +#include <string.h> +#include <stdio.h> + +#include "blake2.h" +#include "blake2-impl.h" + +static const uint64_t blake2b_IV[8] = +{ + 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, + 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, + 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, + 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL +}; + +static const uint8_t blake2b_sigma[12][16] = +{ + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } , + { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } , + { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } , + { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } , + { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } , + { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } , + { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } , + { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } , + { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } , + { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } , + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } , + { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } +}; + + +static void blake2b_set_lastnode( blake2b_state *S ) +{ + S->f[1] = (uint64_t)-1; +} + +/* Some helper functions, not necessarily useful */ +static int blake2b_is_lastblock( const blake2b_state *S ) +{ + return S->f[0] != 0; +} + +static void blake2b_set_lastblock( blake2b_state *S ) +{ + if( S->last_node ) blake2b_set_lastnode( S ); + + S->f[0] = (uint64_t)-1; +} + +static void blake2b_increment_counter( blake2b_state *S, const uint64_t inc ) +{ + S->t[0] += inc; + S->t[1] += ( S->t[0] < inc ); +} + +static void blake2b_init0( blake2b_state *S ) +{ + size_t i; + memset( S, 0, sizeof( blake2b_state ) ); + + for( i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i]; +} + +/* init xors IV with input parameter block */ +int blake2b_init_param( blake2b_state *S, const blake2b_param *P ) +{ + const uint8_t *p = ( const uint8_t * )( P ); + size_t i; + + blake2b_init0( S ); + + /* IV XOR ParamBlock */ + for( i = 0; i < 8; ++i ) + S->h[i] ^= load64( p + sizeof( S->h[i] ) * i ); + + S->outlen = P->digest_length; + return 0; +} + + + +int blake2b_init( blake2b_state *S, size_t outlen ) +{ + blake2b_param P[1]; + + if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1; + + P->digest_length = (uint8_t)outlen; + P->key_length = 0; + P->fanout = 1; + P->depth = 1; + store32( &P->leaf_length, 0 ); + store32( &P->node_offset, 0 ); + store32( &P->xof_length, 0 ); + P->node_depth = 0; + P->inner_length = 0; + memset( P->reserved, 0, sizeof( P->reserved ) ); + memset( P->salt, 0, sizeof( P->salt ) ); + memset( P->personal, 0, sizeof( P->personal ) ); + return blake2b_init_param( S, P ); +} + + +int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen ) +{ + blake2b_param P[1]; + + if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1; + + if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1; + + P->digest_length = (uint8_t)outlen; + P->key_length = (uint8_t)keylen; + P->fanout = 1; + P->depth = 1; + store32( &P->leaf_length, 0 ); + store32( &P->node_offset, 0 ); + store32( &P->xof_length, 0 ); + P->node_depth = 0; + P->inner_length = 0; + memset( P->reserved, 0, sizeof( P->reserved ) ); + memset( P->salt, 0, sizeof( P->salt ) ); + memset( P->personal, 0, sizeof( P->personal ) ); + + if( blake2b_init_param( S, P ) < 0 ) return -1; + + { + uint8_t block[BLAKE2B_BLOCKBYTES]; + memset( block, 0, BLAKE2B_BLOCKBYTES ); + memcpy( block, key, keylen ); + blake2b_update( S, block, BLAKE2B_BLOCKBYTES ); + secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */ + } + return 0; +} + +#define G(r,i,a,b,c,d) \ + do { \ + a = a + b + m[blake2b_sigma[r][2*i+0]]; \ + d = rotr64(d ^ a, 32); \ + c = c + d; \ + b = rotr64(b ^ c, 24); \ + a = a + b + m[blake2b_sigma[r][2*i+1]]; \ + d = rotr64(d ^ a, 16); \ + c = c + d; \ + b = rotr64(b ^ c, 63); \ + } while(0) + +#define ROUND(r) \ + do { \ + G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \ + G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \ + G(r,2,v[ 2],v[ 6],v[10],v[14]); \ + G(r,3,v[ 3],v[ 7],v[11],v[15]); \ + G(r,4,v[ 0],v[ 5],v[10],v[15]); \ + G(r,5,v[ 1],v[ 6],v[11],v[12]); \ + G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \ + G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \ + } while(0) + +static void blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] ) +{ + uint64_t m[16]; + uint64_t v[16]; + size_t i; + + for( i = 0; i < 16; ++i ) { + m[i] = load64( block + i * sizeof( m[i] ) ); + } + + for( i = 0; i < 8; ++i ) { + v[i] = S->h[i]; + } + + v[ 8] = blake2b_IV[0]; + v[ 9] = blake2b_IV[1]; + v[10] = blake2b_IV[2]; + v[11] = blake2b_IV[3]; + v[12] = blake2b_IV[4] ^ S->t[0]; + v[13] = blake2b_IV[5] ^ S->t[1]; + v[14] = blake2b_IV[6] ^ S->f[0]; + v[15] = blake2b_IV[7] ^ S->f[1]; + + ROUND( 0 ); + ROUND( 1 ); + ROUND( 2 ); + ROUND( 3 ); + ROUND( 4 ); + ROUND( 5 ); + ROUND( 6 ); + ROUND( 7 ); + ROUND( 8 ); + ROUND( 9 ); + ROUND( 10 ); + ROUND( 11 ); + + for( i = 0; i < 8; ++i ) { + S->h[i] = S->h[i] ^ v[i] ^ v[i + 8]; + } +} + +#undef G +#undef ROUND + +int blake2b_update( blake2b_state *S, const void *pin, size_t inlen ) +{ + const unsigned char * in = (const unsigned char *)pin; + if( inlen > 0 ) + { + size_t left = S->buflen; + size_t fill = BLAKE2B_BLOCKBYTES - left; + if( inlen > fill ) + { + S->buflen = 0; + memcpy( S->buf + left, in, fill ); /* Fill buffer */ + blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); + blake2b_compress( S, S->buf ); /* Compress */ + in += fill; inlen -= fill; + while(inlen > BLAKE2B_BLOCKBYTES) { + blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES); + blake2b_compress( S, in ); + in += BLAKE2B_BLOCKBYTES; + inlen -= BLAKE2B_BLOCKBYTES; + } + } + memcpy( S->buf + S->buflen, in, inlen ); + S->buflen += inlen; + } + return 0; +} + +int blake2b_final( blake2b_state *S, void *out, size_t outlen ) +{ + uint8_t buffer[BLAKE2B_OUTBYTES] = {0}; + size_t i; + + if( out == NULL || outlen < S->outlen ) + return -1; + + if( blake2b_is_lastblock( S ) ) + return -1; + + blake2b_increment_counter( S, S->buflen ); + blake2b_set_lastblock( S ); + memset( S->buf + S->buflen, 0, BLAKE2B_BLOCKBYTES - S->buflen ); /* Padding */ + blake2b_compress( S, S->buf ); + + for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */ + store64( buffer + sizeof( S->h[i] ) * i, S->h[i] ); + + memcpy( out, buffer, S->outlen ); + secure_zero_memory(buffer, sizeof(buffer)); + return 0; +} + +/* inlen, at least, should be uint64_t. Others can be size_t. */ +int blake2b( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ) +{ + blake2b_state S[1]; + + /* Verify parameters */ + if ( NULL == in && inlen > 0 ) return -1; + + if ( NULL == out ) return -1; + + if( NULL == key && keylen > 0 ) return -1; + + if( !outlen || outlen > BLAKE2B_OUTBYTES ) return -1; + + if( keylen > BLAKE2B_KEYBYTES ) return -1; + + if( keylen > 0 ) + { + if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1; + } + else + { + if( blake2b_init( S, outlen ) < 0 ) return -1; + } + + blake2b_update( S, ( const uint8_t * )in, inlen ); + blake2b_final( S, out, outlen ); + return 0; +} + +int blake2( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ) { + return blake2b(out, outlen, in, inlen, key, keylen); +} + +#if defined(SUPERCOP) +int crypto_hash( unsigned char *out, unsigned char *in, unsigned long long inlen ) +{ + return blake2b( out, BLAKE2B_OUTBYTES, in, inlen, NULL, 0 ); +} +#endif diff --git a/src/cmd/login/login.c b/src/cmd/login/login.c new file mode 100644 index 0000000..3e77ac4 --- /dev/null +++ b/src/cmd/login/login.c @@ -0,0 +1,189 @@ +/* + * Copyright (c) 2025 Ian Marco Moffett and L5 engineers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/iotap.h> +#include <sys/spawn.h> +#include <sys/wait.h> +#include <stddef.h> +#include <ctype.h> +#include <fcntl.h> +#include <errno.h> +#include <unistd.h> +#include <stdio.h> +#include <stdbool.h> +#include <string.h> +#include "blake2.h" + +#define DEFAULT_SHELL "/usr/bin/hush" + +/* Max lengths */ +#define USERNAME_MAX 128 +#define PASSWORD_MAX 256 + +/* + * Read input from an input device + * + * @buf: Buffer to fill with input + * @maxlen: Max length of buffer + * @bool: True if characters should be plaintext + */ +static void +read_input(char *buf, size_t maxlen, bool show) +{ + ssize_t retval; + size_t i = 0; + char c = '\0'; + char vis_c; + struct iotap_msg msg = { + .opcode = IOTAP_OPC_READ, + .buf = &c, + .len = 1 + }; + + if (buf == NULL) { + return; + } + + /* Zero for security */ + memset(buf, 0, maxlen); + + /* + * Read all the input until we hit a '\n' + */ + do { + retval = iotap_mux("input.igkbd", &msg); + if (retval < 0) { + continue; + } + + if (!isascii(c)) { + continue; + } + + /* Don't overwrite prompt */ + if (c == '\b' && i == 0) { + continue; + } + + /* Is this a backspace? */ + if (c == '\b' && i > 0) { + buf[--i] = '\0'; + write(STDOUT_FILENO, &c, 1); + continue; + } + + if (c != '\n' && i < maxlen) { + vis_c = (show) ? c : '*'; + buf[i++] = c; + write(STDOUT_FILENO, &vis_c, 1); + } + + } while (c != '\n'); + printf("\n"); +} + +static int +auth(const char *username, char *hash) +{ + char passwd_path[128]; + char real_hash[BLAKE2B_OUTBYTES]; + int fd, retval = 0; + + /* Get the real password hash */ + snprintf( + passwd_path, + sizeof(passwd_path), + "/ucred/%s/passwd", + username + ); + + fd = open(passwd_path, O_RDONLY); + if (fd < 0) { + return fd; + } + + read(fd, real_hash, BLAKE2B_OUTBYTES); + if (memcmp(hash, real_hash, BLAKE2B_OUTBYTES) != 0) { + retval = -EPERM; + } + + close(fd); + return retval; +} + +int +main(void) +{ + char shell_path[] = DEFAULT_SHELL; + char *argv_dmmy[] = { DEFAULT_SHELL, NULL }; + char login[USERNAME_MAX]; + char password[PASSWORD_MAX]; + char hash[BLAKE2B_OUTBYTES]; + pid_t shell_pid; + int error; + + printf("- the points have aligned -\n"); + printf("** authenticate yourself **\n"); + + for (;;) { + /* Get the username */ + printf("login: "); + read_input(login, sizeof(login), true); + + /* Get the password */ + printf("password: "); + read_input(password, sizeof(password), false); + + /* + * We need to hash this right away and keep the plaintext + * password in memory as little as possible + */ + blake2b( + hash, + BLAKE2B_OUTBYTES, + password, + strlen(password), + NULL, + 0 + ); + + memset(password, 0, sizeof(password)); + error = auth(login, hash); + if (error < 0) { + printf("error: bad login\n"); + continue; + } + + break; + } + + shell_pid = spawn(shell_path, argv_dmmy); + waitpid(shell_pid, NULL, 0); + return 0; +} diff --git a/src/data/etc/passwd.master b/src/data/etc/passwd.master new file mode 100644 index 0000000..07d3d57 --- /dev/null +++ b/src/data/etc/passwd.master @@ -0,0 +1 @@ +sv:0 diff --git a/src/data/ucred/sv/note b/src/data/ucred/sv/note new file mode 100644 index 0000000..056a272 --- /dev/null +++ b/src/data/ucred/sv/note @@ -0,0 +1 @@ +Supervisor user diff --git a/src/data/ucred/sv/passwd b/src/data/ucred/sv/passwd new file mode 100644 index 0000000..0023460 --- /dev/null +++ b/src/data/ucred/sv/passwd @@ -0,0 +1 @@ +hjNPQt*DϔMAwnX${9X^D<:V_}Ta)S
\ No newline at end of file diff --git a/src/lib/libc/include/stdio.h b/src/lib/libc/include/stdio.h index c8b7c10..31d59a5 100644 --- a/src/lib/libc/include/stdio.h +++ b/src/lib/libc/include/stdio.h @@ -53,6 +53,13 @@ int vsnprintf(char *s, size_t size, const char *fmt, va_list ap); int snprintf(char *s, size_t size, const char *fmt, ...); /* + * Write a character to stdout + * + * @c: Character to write + */ +int putchar(int c); + +/* * Print a formatted string */ int printf(const char *__restrict fmt, ...); diff --git a/src/lib/libc/include/string.h b/src/lib/libc/include/string.h index 1572400..360af12 100644 --- a/src/lib/libc/include/string.h +++ b/src/lib/libc/include/string.h @@ -93,4 +93,13 @@ void *memset(void *s, int c, size_t n); */ char *itoa(int64_t value, char *buf, int base); +/* + * Compare two byte streams + * + * @s1: Stream one + * @s2: Stream two + * @n: Max bytes to compare + */ +int memcmp(const void *s1, const void *s2, size_t n); + #endif /* _STRING_H */ diff --git a/src/lib/libc/include/unistd.h b/src/lib/libc/include/unistd.h index fa7f951..b668d5b 100644 --- a/src/lib/libc/include/unistd.h +++ b/src/lib/libc/include/unistd.h @@ -48,6 +48,15 @@ int open(const char *path, int flags); /* + * Close a file using its file descriptor + * + * @fd: File descriptor to close + * + * Returns zero on success + */ +int close(int fd); + +/* * POSIX write system call * * @fd: File descriptor to write at @@ -56,4 +65,13 @@ int open(const char *path, int flags); */ ssize_t write(int fd, const void *buf, size_t count); +/* + * POSIX read system call + * + * @fd: File descriptor to read + * @buf: Buffer to read into + * @count: Number of bytes to read + */ +ssize_t read(int fd, void *buf, size_t count); + #endif /* _UNISTD_H */ diff --git a/src/lib/libc/src/stdio/puts.c b/src/lib/libc/src/stdio/puts.c index 6446e10..021bf89 100644 --- a/src/lib/libc/src/stdio/puts.c +++ b/src/lib/libc/src/stdio/puts.c @@ -46,3 +46,10 @@ puts(const char *s) write(STDOUT_FILENO, &ch_lf, 1); return slen + 1; } + +int +putchar(int c) +{ + write(STDOUT_FILENO, &c, 1); + return c; +} diff --git a/src/lib/libc/src/string/memcmp.c b/src/lib/libc/src/string/memcmp.c new file mode 100644 index 0000000..2dab02d --- /dev/null +++ b/src/lib/libc/src/string/memcmp.c @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2025 Ian Marco Moffett and L5 engineers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <string.h> + +int +memcmp(const void *s1, const void *s2, size_t n) +{ + if (n != 0) { + const unsigned char *p1 = s1, *p2 = s2; + + do { + if (*p1++ != *p2++) { + return (*--p1 - *--p2); + } + } while (--n != 0); + } + return 0; +} diff --git a/src/lib/libc/src/unistd/close.c b/src/lib/libc/src/unistd/close.c new file mode 100644 index 0000000..125bba0 --- /dev/null +++ b/src/lib/libc/src/unistd/close.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2025 Ian Marco Moffett and L5 engineers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/syscall.h> +#include <unistd.h> + +int +close(int fd) +{ + return syscall( + SYS_close, + fd + ); +} diff --git a/src/lib/libc/src/unistd/read.c b/src/lib/libc/src/unistd/read.c new file mode 100644 index 0000000..5c45c09 --- /dev/null +++ b/src/lib/libc/src/unistd/read.c @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2025 Ian Marco Moffett and L5 engineers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/syscall.h> +#include <unistd.h> + +ssize_t +read(int fd, void *buf, size_t count) +{ + if (buf == NULL || count == 0) { + return -1; + } + + return syscall( + SYS_read, + fd, + (uintptr_t)buf, + count + ); +} diff --git a/src/sys/arch/amd64/conf/GENERIC b/src/sys/arch/amd64/conf/GENERIC index 93919d8..f402941 100644 --- a/src/sys/arch/amd64/conf/GENERIC +++ b/src/sys/arch/amd64/conf/GENERIC @@ -2,3 +2,7 @@ // as a method. Some firmware (e.g., on some laptops) // may not handle these well and hang. option I8042_REBOOT yes + +// Controls if the i8042 should be polled +// rather than dependent on interrupts +option I8042_POLL no diff --git a/src/sys/arch/amd64/cpu/cpu_mp.c b/src/sys/arch/amd64/cpu/cpu_mp.c index ae7b367..7b691e3 100644 --- a/src/sys/arch/amd64/cpu/cpu_mp.c +++ b/src/sys/arch/amd64/cpu/cpu_mp.c @@ -56,10 +56,19 @@ static volatile struct limine_smp_request g_smp_req = { .revision = 0 }; +static void +cpu_idle(void *) +{ + for (;;) { + __ASMV("pause"); + } +} + __dead static void ap_entry(struct limine_smp_info *) { struct pcore *pcore; + struct proc *curidle; spinlock_acquire(&lock); pcore = kalloc(sizeof(*pcore)); @@ -76,9 +85,10 @@ ap_entry(struct limine_smp_info *) corelist[ncores_up - 1] = pcore; atomic_inc_64(&ncores_up); + proc_ktd(&curidle, cpu_idle); spinlock_release(&lock); - md_proc_yield(); + md_proc_idle(); __builtin_unreachable(); for (;;); } @@ -106,6 +116,7 @@ bsp_ap_startup(void) struct limine_smp_response *resp = g_smp_req.response; struct limine_smp_info **cpus; struct mdcore *mdcore; + struct proc *curidle; uint32_t ncores, tmp; /* Sanity check */ @@ -139,6 +150,7 @@ bsp_ap_startup(void) printf("mp: bringing APs online...\n"); mdcore = &g_bsp.md; + proc_ktd(&curidle, cpu_idle); for (int i = 0; i < ncores; ++i) { if (mdcore->apic_id == cpus[i]->lapic_id) { continue; diff --git a/src/sys/arch/amd64/cpu/trap.c b/src/sys/arch/amd64/cpu/trap.c index 921a036..ae3a0cc 100644 --- a/src/sys/arch/amd64/cpu/trap.c +++ b/src/sys/arch/amd64/cpu/trap.c @@ -39,6 +39,7 @@ #include <sys/syslog.h> #include <sys/syscall.h> #include <machine/trap.h> +#include <string.h> /* * Trap type to type string conversion table @@ -154,6 +155,7 @@ trap_syscall(struct trapframe *tf) struct syscall_domain *scdp; struct syscall_win *scwp; struct proc *self; + struct md_pcb *pcbp; struct syscall_args scargs = { .arg[0] = tf->rdi, .arg[1] = tf->rsi, @@ -179,6 +181,9 @@ trap_syscall(struct trapframe *tf) return; } + pcbp = &self->pcb; + memcpy(&pcbp->tf, tf, sizeof(pcbp->tf)); + if (tf->rax < scwp->nimpl && tf->rax > 0) { tf->rax = scwp->sctab[tf->rax](&scargs); } diff --git a/src/sys/arch/amd64/isa/i8042.c b/src/sys/arch/amd64/isa/i8042.c index 9417206..6ffea73 100644 --- a/src/sys/arch/amd64/isa/i8042.c +++ b/src/sys/arch/amd64/isa/i8042.c @@ -40,6 +40,12 @@ #include <machine/pio.h> #include <stdbool.h> +#if defined(__I8042_POLL) +#define I8042_POLL __I8042_POLL +#else +#define I8042_POLL 0 +#endif /* !I8042_POLL */ + #define RING_NENT 16 /* @@ -58,6 +64,7 @@ struct keybuf { /* I/O tap forward declarations */ static struct iotap_ops tap_port0_ops; static struct iotap_desc tap_port0; +static struct proc *kbd_poll_td; /* Key states */ static bool shift_key = false; @@ -350,6 +357,20 @@ i8042_irq(struct intr_hand *hp) return 1; } +static void +i8042_poll(void *p) +{ + char c; + uint8_t scancode; + + for (;;) { + scancode = i8042_read(); + if (i8042_getc(scancode, &c) == 0) { + keybuf_enter(&buf, c); + } + } +} + /* * Initialize i8042 interrupts (IRQ 1) */ @@ -399,8 +420,18 @@ i8042_init(struct module *modp) i8042_write(true, I8042_DISABLE_PORT1); i8042_read(); - /* Initialize interrupts and taps */ - i8042_init_intr(); + /* + * If we are configured to do interrupts, enable + * them. Otherwise, start a kernel thread and + * poll. + */ + if (!I8042_POLL) { + i8042_init_intr(); + } else { + proc_ktd(&kbd_poll_td, i8042_poll); + } + + /* Enable I/O taps */ i8042_init_tap(); /* Enable the keyboard */ diff --git a/src/sys/arch/amd64/os/os_proc.c b/src/sys/arch/amd64/os/os_proc.c index 93e5d73..23948ab 100644 --- a/src/sys/arch/amd64/os/os_proc.c +++ b/src/sys/arch/amd64/os/os_proc.c @@ -112,8 +112,14 @@ md_proc_init(struct proc *procp, int flags) return error; } - ds = USER_DS | 3; - cs = USER_CS | 3; + if (ISSET(flags, SPAWN_KTD)) { + ds = KERNEL_DS; + cs = KERNEL_CS; + procp->flags |= PROC_KTD; + } else { + ds = USER_DS | 3; + cs = USER_CS | 3; + } /* * Set up the mapping specifier, we'll use zero @@ -146,7 +152,7 @@ md_proc_init(struct proc *procp, int flags) * Process idle loop */ __dead void -md_proc_yield(void) +md_proc_idle(void) { struct proc *proc; struct pcore *core = this_core(); @@ -161,11 +167,6 @@ md_proc_yield(void) */ for (;;) { lapic_timer_oneshot_us(9000); - error = sched_deq(&core->scq, &proc); - if (error == 0) { - core->curproc = proc; - md_proc_kick(proc); - } __ASMV("sti; hlt"); } } @@ -288,7 +289,7 @@ md_proc_kill(struct proc *procp, int flags) /* If this is us, spin time */ if (self->pid == procp->pid) { core->curproc = NULL; - md_proc_yield(); + md_proc_idle(); } return 0; diff --git a/src/sys/compat/unix/os/os_filedesc.c b/src/sys/compat/unix/os/os_filedesc.c index 673701f..952f022 100644 --- a/src/sys/compat/unix/os/os_filedesc.c +++ b/src/sys/compat/unix/os/os_filedesc.c @@ -138,3 +138,14 @@ sys_read(struct syscall_args *scargs) kfree(kbuf); return (error == 0) ? retval : error; } + +/* + * ARG0: FD + */ +scret_t +sys_close(struct syscall_args *scargs) +{ + int fd = SCARG(scargs, int, 0); + + return fd_close(fd); +} diff --git a/src/sys/compat/unix/os/proc_syscall.c b/src/sys/compat/unix/os/proc_syscall.c index 1838761..634ebf3 100644 --- a/src/sys/compat/unix/os/proc_syscall.c +++ b/src/sys/compat/unix/os/proc_syscall.c @@ -46,5 +46,5 @@ sys_exit(struct syscall_args *scargs) status = SCARG(scargs, int, 0); proc_kill(core->curproc, status); - md_proc_yield(); /* unreachable */ + md_proc_idle(); /* unreachable */ } diff --git a/src/sys/include/compat/unix/syscall.h b/src/sys/include/compat/unix/syscall.h index be351e8..a031872 100644 --- a/src/sys/include/compat/unix/syscall.h +++ b/src/sys/include/compat/unix/syscall.h @@ -68,6 +68,11 @@ scret_t sys_open(struct syscall_args *scargs); */ scret_t sys_read(struct syscall_args *scargs); +/* + * Close a file + */ +scret_t sys_close(struct syscall_args *scargs); + #ifdef _NEED_UNIX_SCTAB scret_t(*g_unix_sctab[])(struct syscall_args *) = { [SYS_none] = NULL, @@ -83,7 +88,8 @@ scret_t(*g_unix_sctab[])(struct syscall_args *) = { [SYS_reboot] = sys_reboot, [SYS_waitpid] = sys_waitpid, [SYS_dmsio] = sys_dmsio, - [SYS_read] = sys_read + [SYS_read] = sys_read, + [SYS_close] = sys_close }; #endif /* !_NEED_UNIX_SCTAB */ diff --git a/src/sys/include/os/filedesc.h b/src/sys/include/os/filedesc.h index 8cdf161..a6a2ac5 100644 --- a/src/sys/include/os/filedesc.h +++ b/src/sys/include/os/filedesc.h @@ -39,11 +39,13 @@ struct proc; * Represents a file descriptor * * @fdno: File descriptor index + * @off: Current file offset * @vp: Vnode this fd is linked with * @mode: File attributes */ struct filedesc { int fdno; + off_t off; struct vnode *vp; mode_t mode; }; @@ -82,6 +84,16 @@ int fdtab_init(struct proc *procp); int fd_open(const char *path, mode_t mode); /* + * Close a file + * + * @fd: File descriptor to close + * + * Returns zero on success, otherwise a less than + * zero value on failure + */ +int fd_close(int fd); + +/* * Write to a file descriptor * * @fd: File descriptor to write to diff --git a/src/sys/include/os/vnode.h b/src/sys/include/os/vnode.h index 868e137..1bef1e2 100644 --- a/src/sys/include/os/vnode.h +++ b/src/sys/include/os/vnode.h @@ -72,11 +72,13 @@ struct vop_lookup_args { * * @data: Buffer containing I/O data * @len: Length of buffer + * @off: Offset of operation * @vp: Current vnode */ struct vop_rw_data { void *data; size_t len; + off_t off; struct vnode *vp; }; @@ -86,6 +88,7 @@ struct vop_rw_data { */ struct vop { int(*lookup)(struct vop_lookup_args *args); + int(*reclaim)(struct vnode *vp, int flags); ssize_t(*write)(struct vop_rw_data *data); ssize_t(*read)(struct vop_rw_data *data); }; @@ -141,23 +144,36 @@ int vfs_vrel(struct vnode *vp, int flags); * * @vp: Vnode to write to * @data: Data to write + * @off: Offset to write at * @len: Length of bytes to write * * Returns the number of bytes written on success, otherwise * a less than zero value on failure. */ -ssize_t vop_write(struct vnode *vp, char *data, size_t len); +ssize_t vop_write(struct vnode *vp, char *data, off_t off, size_t len); /* * Wrapper for the read write callback * * @vp: Vnode to read from * @data: Read data written here + * @off: Offset to read at * @len: Length of bytes to read * * Returns the number of bytes read on success, otherwise * a less than zero value on failure. */ -ssize_t vop_read(struct vnode *vp, char *data, size_t len); +ssize_t vop_read(struct vnode *vp, char *data, off_t off, size_t len); + +/* + * Reclaim the resources tied to a specific vnode + * + * @vp: Vnode to reclaim + * @flags: Optional flags + * + * Returns zero on success, otherwise a less than zero value + * on failure. + */ +int vop_reclaim(struct vnode *vp, int flags); #endif /* !_OS_VNODE_H_ */ diff --git a/src/sys/include/sys/proc.h b/src/sys/include/sys/proc.h index 618ddf3..a547233 100644 --- a/src/sys/include/sys/proc.h +++ b/src/sys/include/sys/proc.h @@ -101,6 +101,10 @@ struct proc { #define PROC_EXITING BIT(0) /* Process is exiting */ #define PROC_SLEEPING BIT(1) /* Process is sleeping */ +#define PROC_KTD BIT(2) /* Process is kernel thread */ + +/* Flags for PROC_SPAWN */ +#define SPAWN_KTD BIT(0) /* Spawn kernel thread */ /* * Initialize a process into a basic minimal @@ -136,6 +140,14 @@ struct proc *proc_self(void); int proc_add_range(struct proc *procp, vaddr_t va, paddr_t pa, size_t len); /* + * Spawn a kernel thread + * + * @procp_res: Result is written here + * @fn: Function where kernel thread should end up + */ +int proc_ktd(struct proc **procp_res, void(*fn)(void *)); + +/* * Kill a process with a specific status code * * @procp: Process to kill @@ -210,8 +222,7 @@ int proc_check_addr(struct proc *proc, uintptr_t addr, size_t len); * * @proc: Process to put to sleep * - * Returns zero if the address is within the process bounds, - * otherwise a less than zero value on failure. + * Returns zero on success */ int proc_sleep(struct proc *proc); @@ -244,7 +255,7 @@ void md_proc_sleep(void); * Put the current process into a halt loop * until the next one runs. */ -__dead void md_proc_yield(void); +__dead void md_proc_idle(void); /* * Kick a process into a user context diff --git a/src/sys/include/sys/syscall.h b/src/sys/include/sys/syscall.h index 1138029..77b7505 100644 --- a/src/sys/include/sys/syscall.h +++ b/src/sys/include/sys/syscall.h @@ -57,6 +57,7 @@ #define SYS_waitpid 0x0C /* wait for child to exit */ #define SYS_dmsio 0x0D /* DMS I/O */ #define SYS_read 0x0E /* read a file descriptor */ +#define SYS_close 0x0F /* close a file */ typedef __ssize_t scret_t; typedef __ssize_t scarg_t; diff --git a/src/sys/os/os_filedes.c b/src/sys/os/os_filedes.c index 9ba0c63..95452af 100644 --- a/src/sys/os/os_filedes.c +++ b/src/sys/os/os_filedes.c @@ -197,6 +197,32 @@ fd_open(const char *path, mode_t mode) return fd->fdno; } +int +fd_close(int fd) +{ + struct filedesc *fdp; + struct proc *proc = proc_self(); + struct vnode *vp; + + if (fd < 0) { + return -EBADF; + } + + if (proc == NULL) { + return -EIO; + } + + if ((fdp = fd_get(proc, fd)) == NULL) { + return -EIO; + } + + vp = fdp->vp; + proc->fdtab[fdp->fdno] = NULL; + + kfree(fdp); + return vop_reclaim(vp, 0); +} + /* * Initialize file descriptor table */ @@ -227,8 +253,9 @@ write(int fd, const void *buf, size_t count) { struct proc *self = proc_self(); struct filedesc *fdp; - int error; + ssize_t retval; char kbuf[1024]; + int error; if (self == NULL) { return -ESRCH; @@ -267,7 +294,13 @@ write(int fd, const void *buf, size_t count) if (fdp->vp == NULL) { return -EIO; } - return vop_write(fdp->vp, kbuf, count); + retval = vop_write(fdp->vp, kbuf, fdp->off, count); + if (retval <= 0) { + return retval; + } + + /* Move away from where we wrote */ + fdp->off += count; } return count; @@ -278,6 +311,7 @@ read(int fd, void *buf, size_t count) { struct proc *self = proc_self(); struct filedesc *fdp; + ssize_t retval; int error; if (buf == NULL) { @@ -304,7 +338,14 @@ read(int fd, void *buf, size_t count) return -EIO; } - return vop_read(fdp->vp, buf, count); + /* Read the file */ + retval = vop_read(fdp->vp, buf, fdp->off, count); + if (retval <= 0) { + return retval; + } + + fdp->off += count; + return retval; } /* diff --git a/src/sys/os/os_init.c b/src/sys/os/os_init.c index 43468b3..8287f1a 100644 --- a/src/sys/os/os_init.c +++ b/src/sys/os/os_init.c @@ -73,14 +73,15 @@ main(void) vm_init(); cpu_init(&g_bsp); - bsp_ap_startup(); vfs_init(); ns_init(); + sched_init(); + bsp_ap_startup(); + /* Initialize generic modules */ __MODULES_INIT(MODTYPE_GENERIC); - sched_init(); core = this_core(); proc_init(&g_rootproc, 0); core->curproc = &g_rootproc; diff --git a/src/sys/os/os_omar.c b/src/sys/os/os_omar.c index 9ad3739..8e63ae7 100644 --- a/src/sys/os/os_omar.c +++ b/src/sys/os/os_omar.c @@ -258,7 +258,11 @@ initrd_read(struct vop_rw_data *data) len = MIN(data->len, np->size); for (int i = 0; i < len; ++i) { - dest[i] = src[i]; + if ((i + data->off) >= np->size) { + /* End of file */ + return 0; + } + dest[i] = src[data->off + i]; } return len; diff --git a/src/sys/os/os_proc.c b/src/sys/os/os_proc.c index 582c077..a8f49d7 100644 --- a/src/sys/os/os_proc.c +++ b/src/sys/os/os_proc.c @@ -393,6 +393,36 @@ proc_spawn(const char *path, struct penv_blk *envbp) return proc->pid; } +int +proc_ktd(struct proc **procp_res, void(*fn)(void *)) +{ + struct proc *proc; + struct pcore *core; + + if (procp_res == NULL || fn == NULL) { + return -EINVAL; + } + + proc = kalloc(sizeof(*proc)); + if (proc == NULL) { + return -ENOMEM; + } + + core = cpu_sched(); + if (core == NULL) { + kfree(proc); + return -EIO; + } + + proc_init(proc, SPAWN_KTD); + md_set_ip(proc, (uintptr_t)fn); + sched_enq(&core->scq, proc); + + *procp_res = proc; + TAILQ_INSERT_TAIL(&procq, proc, lup_link); + return 0; +} + /* * ARG0: Pathname to spawn * ARG1: Process environment block diff --git a/src/sys/os/vfs_subr.c b/src/sys/os/vfs_subr.c index 47ed468..6cb7767 100644 --- a/src/sys/os/vfs_subr.c +++ b/src/sys/os/vfs_subr.c @@ -145,7 +145,7 @@ vfs_cmp_cnt(const char *path) } ssize_t -vop_write(struct vnode *vp, char *data, size_t len) +vop_write(struct vnode *vp, char *data, off_t off, size_t len) { struct vop_rw_data rwdata; struct vop *vops; @@ -170,11 +170,12 @@ vop_write(struct vnode *vp, char *data, size_t len) rwdata.data = data; rwdata.len = len; rwdata.vp = vp; + rwdata.off = off; return vops->write(&rwdata); } ssize_t -vop_read(struct vnode *vp, char *data, size_t len) +vop_read(struct vnode *vp, char *data, off_t off, size_t len) { struct vop_rw_data rwdata; struct vop *vops; @@ -199,5 +200,27 @@ vop_read(struct vnode *vp, char *data, size_t len) rwdata.data = data; rwdata.len = len; rwdata.vp = vp; + rwdata.off = off; return vops->read(&rwdata); } + +int +vop_reclaim(struct vnode *vp, int flags) +{ + struct vop *vops; + + if (vp == NULL) { + return -EINVAL; + } + + /* Grab the virtual operations */ + if ((vops = vp->vops) == NULL) { + return -EIO; + } + + if (vops->reclaim == NULL) { + return -ENOTSUP; + } + + return vops->reclaim(vp, flags); +} |