libk  Check-in [5560c2725b]

Overview
Comment:add first iteration of knum header
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 5560c2725bf4b48a5735168c0300604d90c87e6f7b886eeb314f4242f3e4d604
User & Date: lexi on 2019-08-22 01:37:08
Other Links: manifest | tags
Context
2019-08-22
02:52
removed sneaky segfault in x86-64 syscall fn where %r8 (the register that contains the pointer to the syscall arguments from the C syscall wrapper, which need to be copied into the correct registers before the kernel is invoked) gets overwritten if the syscall valency > 5, because of overlapping ccall and syscall ABI argument registers - r8 is clobbered by argument 5 and any further attempts to use it as a ptr segfault at best. also modified the report function so that it immediate cancels compilation if a sub-process reports failure. changed allocator function signatures so they can return a condition code if the kernel reports an error; updated example code so it compiles and runs without fault. check-in: e50a476efe user: lexi tags: trunk
01:37
add first iteration of knum header check-in: 5560c2725b user: lexi tags: trunk
00:03
insert explicit cast to hopefully silence compiler warnings on some systems check-in: 7c202a8b17 user: lexi tags: trunk
Changes

Modified build.sh from [b43f4693b2] to [d7b4907031].

     1      1   #!/usr/bin/env bash
            2  +# build.sh - libk build script
            3  +
            4  +# TODO when compiling C code, first check
            5  +#      if an assembly override exists for
            6  +#      the target platform and if there's
            7  +#      one, ignore the C and assemble the
            8  +#      override file instead.
            9  +
     2     10   export to=${to:-out}
     3     11   source global/common.sh
     4     12   
     5     13   if test "$os$arch$bits" = ""; then
     6     14   	say "set the following environment variables to the appropriate atoms describing your system. for help determining the appropriate atoms, see libk.md"
     7     15   	say ' - $os={lin|fbsd|hai|osx…}'
     8     16   	say ' - $arch={x86|arm|ia64|mips…}'

Added mod/knum/num.h version [982620d249].

            1  +#ifndef KInum
            2  +#define KInum
            3  +
            4  +#ifdef __cplusplus
            5  +	extern "C" {
            6  +#endif
            7  +
            8  +#include <k/type.h>
            9  +
           10  +typedef enum kncond {
           11  +	kncond_ok = kncond_id,
           12  +	kncond_saturate,
           13  +	kncond_wrap,
           14  +
           15  +	/* error states */
           16  +	kncond_fail,
           17  +	kncond_fail_io,
           18  +	kncond_overflow,
           19  +	kncond_underflow,
           20  +	kncond_bad_domain,
           21  +} kncond;
           22  +
           23  +/* this "hard" random number generator
           24  + * returns a block of random data that
           25  + * can be used safely in cryptographic
           26  + * pseudorandom number algorithms such
           27  + * as e.g. knrands() */
           28  +
           29  +enum kncond
           30  +knrandh(u8* mem, sz count);
           31  +
           32  +/* we define the largest available native
           33  + * type as `knword` for convenience; this
           34  + * is the type that the pseudorandom func
           35  + * will use for state and return; */
           36  +typedef unsigned long long knword;
           37  +
           38  +/* to encapsulate the state of the pseudo
           39  + * random number generator,  we define an
           40  + * array as an opaque seed reference type */
           41  +typedef knword knseed [8];
           42  +
           43  +/* this function implements a "soft" RNG
           44  + * that can be safely used as a cryptographic
           45  + * primitive, keeping its state in a value of
           46  + * type of knseed (which needs to be seeded
           47  + * initially by knrandh() or another reliable
           48  + * source of entropy. */
           49  +knword knrands(knseed state);
           50  +
           51  +/* addition and subtraction in C can be
           52  + * complex due to the need to check for
           53  + * underflow and overflow,  so we offer
           54  + * a suite of functions which implement
           55  + * reliable arithmetic over integers of
           56  + * arbitrary bit lengths. functions for 
           57  + * fixed point & floating point numbers
           58  + * should also be added eventually.
           59  + * note that these functions use either
           60  + * two's complement or one's complement
           61  + * for signed */
           62  +
           63  +typedef enum knmode {
           64  +	knmode_saturate = 0x0001, /* 0b001 */
           65  +	knmode_wrap     = 0x0002, /* 0b010 */
           66  +	knmode_fail     = 0x0003, /* 0b011 */
           67  +
           68  +	knmode_signed   = 1 << 2, /* 0b100*/
           69  +	knmode_unsigned = 0 /* default */
           70  +} knmode;
           71  +
           72  +/* it is legal for src and dest to be the
           73  + * same location in memory. this will not
           74  + * produce incorrect output. although the
           75  + * functions formally take pointers to u8,
           76  + * they are converted  appropriately with
           77  + * regard to sign and size within the fn */
           78  +
           79  +kncond kniadd(knmode, sz, u8* dest, u8* src, u8* delta);
           80  +kncond knisub(knmode, sz, u8* dest, u8* src, u8* delta);
           81  +kncond knimul(knmode, sz, u8* dest, u8* src, u8* delta);
           82  +kncond knidiv(knmode, sz,
           83  +	/* output */ u8* ratio, u8* remainder,
           84  +	/*  input */ u8* src, u8* delta);
           85  +
           86  +/* we should probably also offer a bignum
           87  + * type eventually, tho this will need to
           88  + * be integrated with kmem for allocation. */
           89  +
           90  +#ifdef __cplusplus
           91  +	}
           92  +#endif
           93  +
           94  +#endif