Initial Spring 2016 commit.
This commit is contained in:
135
kern/lib/array.c
Normal file
135
kern/lib/array.c
Normal file
@@ -0,0 +1,135 @@
|
||||
/*-
|
||||
* Copyright (c) 2009 The NetBSD Foundation, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by David A. Holland.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
||||
*/
|
||||
|
||||
#define ARRAYINLINE
|
||||
|
||||
#include <types.h>
|
||||
#include <kern/errno.h>
|
||||
#include <lib.h>
|
||||
#include <array.h>
|
||||
|
||||
struct array *
|
||||
array_create(void)
|
||||
{
|
||||
struct array *a;
|
||||
|
||||
a = kmalloc(sizeof(*a));
|
||||
if (a != NULL) {
|
||||
array_init(a);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
void
|
||||
array_destroy(struct array *a)
|
||||
{
|
||||
array_cleanup(a);
|
||||
kfree(a);
|
||||
}
|
||||
|
||||
void
|
||||
array_init(struct array *a)
|
||||
{
|
||||
a->num = a->max = 0;
|
||||
a->v = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
array_cleanup(struct array *a)
|
||||
{
|
||||
/*
|
||||
* Require array to be empty - helps avoid memory leaks since
|
||||
* we don't/can't free anything any contents may be pointing
|
||||
* to.
|
||||
*/
|
||||
ARRAYASSERT(a->num == 0);
|
||||
kfree(a->v);
|
||||
#ifdef ARRAYS_CHECKED
|
||||
a->v = NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
array_preallocate(struct array *a, unsigned num)
|
||||
{
|
||||
void **newptr;
|
||||
unsigned newmax;
|
||||
|
||||
if (num > a->max) {
|
||||
/* Don't touch A until the allocation succeeds. */
|
||||
newmax = a->max;
|
||||
while (num > newmax) {
|
||||
newmax = newmax ? newmax*2 : 4;
|
||||
}
|
||||
|
||||
/*
|
||||
* We don't have krealloc, and it wouldn't be
|
||||
* worthwhile to implement just for this. So just
|
||||
* allocate a new block and copy. (Exercise: what
|
||||
* about this and/or kmalloc makes it not worthwhile?)
|
||||
*/
|
||||
|
||||
newptr = kmalloc(newmax*sizeof(*a->v));
|
||||
if (newptr == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
memcpy(newptr, a->v, a->num*sizeof(*a->v));
|
||||
kfree(a->v);
|
||||
a->v = newptr;
|
||||
a->max = newmax;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
array_setsize(struct array *a, unsigned num)
|
||||
{
|
||||
int result;
|
||||
|
||||
result = array_preallocate(a, num);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
a->num = num;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
array_remove(struct array *a, unsigned index)
|
||||
{
|
||||
unsigned num_to_move;
|
||||
|
||||
ARRAYASSERT(a->num <= a->max);
|
||||
ARRAYASSERT(index < a->num);
|
||||
|
||||
num_to_move = a->num - (index + 1);
|
||||
memmove(a->v + index, a->v + index+1, num_to_move*sizeof(void *));
|
||||
a->num--;
|
||||
}
|
176
kern/lib/bitmap.c
Normal file
176
kern/lib/bitmap.c
Normal file
@@ -0,0 +1,176 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* 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 University 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 UNIVERSITY 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 UNIVERSITY 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Fixed-size array of bits. (Intended for storage management.)
|
||||
*/
|
||||
|
||||
#include <types.h>
|
||||
#include <kern/errno.h>
|
||||
#include <lib.h>
|
||||
#include <bitmap.h>
|
||||
|
||||
/*
|
||||
* It would be a lot more efficient on most platforms to use uint32_t
|
||||
* or unsigned long as the base type for holding bits. But we don't,
|
||||
* because if one uses any data type more than a single byte wide,
|
||||
* bitmap data saved on disk becomes endian-dependent, which is a
|
||||
* severe nuisance.
|
||||
*/
|
||||
#define BITS_PER_WORD (CHAR_BIT)
|
||||
#define WORD_TYPE unsigned char
|
||||
#define WORD_ALLBITS (0xff)
|
||||
|
||||
struct bitmap {
|
||||
unsigned nbits;
|
||||
WORD_TYPE *v;
|
||||
};
|
||||
|
||||
|
||||
struct bitmap *
|
||||
bitmap_create(unsigned nbits)
|
||||
{
|
||||
struct bitmap *b;
|
||||
unsigned words;
|
||||
|
||||
words = DIVROUNDUP(nbits, BITS_PER_WORD);
|
||||
b = kmalloc(sizeof(struct bitmap));
|
||||
if (b == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
b->v = kmalloc(words*sizeof(WORD_TYPE));
|
||||
if (b->v == NULL) {
|
||||
kfree(b);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bzero(b->v, words*sizeof(WORD_TYPE));
|
||||
b->nbits = nbits;
|
||||
|
||||
/* Mark any leftover bits at the end in use */
|
||||
if (words > nbits / BITS_PER_WORD) {
|
||||
unsigned j, ix = words-1;
|
||||
unsigned overbits = nbits - ix*BITS_PER_WORD;
|
||||
|
||||
KASSERT(nbits / BITS_PER_WORD == words-1);
|
||||
KASSERT(overbits > 0 && overbits < BITS_PER_WORD);
|
||||
|
||||
for (j=overbits; j<BITS_PER_WORD; j++) {
|
||||
b->v[ix] |= ((WORD_TYPE)1 << j);
|
||||
}
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
void *
|
||||
bitmap_getdata(struct bitmap *b)
|
||||
{
|
||||
return b->v;
|
||||
}
|
||||
|
||||
int
|
||||
bitmap_alloc(struct bitmap *b, unsigned *index)
|
||||
{
|
||||
unsigned ix;
|
||||
unsigned maxix = DIVROUNDUP(b->nbits, BITS_PER_WORD);
|
||||
unsigned offset;
|
||||
|
||||
for (ix=0; ix<maxix; ix++) {
|
||||
if (b->v[ix]!=WORD_ALLBITS) {
|
||||
for (offset = 0; offset < BITS_PER_WORD; offset++) {
|
||||
WORD_TYPE mask = ((WORD_TYPE)1) << offset;
|
||||
|
||||
if ((b->v[ix] & mask)==0) {
|
||||
b->v[ix] |= mask;
|
||||
*index = (ix*BITS_PER_WORD)+offset;
|
||||
KASSERT(*index < b->nbits);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
KASSERT(0);
|
||||
}
|
||||
}
|
||||
return ENOSPC;
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
void
|
||||
bitmap_translate(unsigned bitno, unsigned *ix, WORD_TYPE *mask)
|
||||
{
|
||||
unsigned offset;
|
||||
*ix = bitno / BITS_PER_WORD;
|
||||
offset = bitno % BITS_PER_WORD;
|
||||
*mask = ((WORD_TYPE)1) << offset;
|
||||
}
|
||||
|
||||
void
|
||||
bitmap_mark(struct bitmap *b, unsigned index)
|
||||
{
|
||||
unsigned ix;
|
||||
WORD_TYPE mask;
|
||||
|
||||
KASSERT(index < b->nbits);
|
||||
bitmap_translate(index, &ix, &mask);
|
||||
|
||||
KASSERT((b->v[ix] & mask)==0);
|
||||
b->v[ix] |= mask;
|
||||
}
|
||||
|
||||
void
|
||||
bitmap_unmark(struct bitmap *b, unsigned index)
|
||||
{
|
||||
unsigned ix;
|
||||
WORD_TYPE mask;
|
||||
|
||||
KASSERT(index < b->nbits);
|
||||
bitmap_translate(index, &ix, &mask);
|
||||
|
||||
KASSERT((b->v[ix] & mask)!=0);
|
||||
b->v[ix] &= ~mask;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
bitmap_isset(struct bitmap *b, unsigned index)
|
||||
{
|
||||
unsigned ix;
|
||||
WORD_TYPE mask;
|
||||
|
||||
bitmap_translate(index, &ix, &mask);
|
||||
return (b->v[ix] & mask);
|
||||
}
|
||||
|
||||
void
|
||||
bitmap_destroy(struct bitmap *b)
|
||||
{
|
||||
kfree(b->v);
|
||||
kfree(b);
|
||||
}
|
161
kern/lib/bswap.c
Normal file
161
kern/lib/bswap.c
Normal file
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* 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 University 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 UNIVERSITY 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 UNIVERSITY 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 <types.h>
|
||||
#include <endian.h>
|
||||
|
||||
/*
|
||||
* Unconditional byte-swap functions.
|
||||
*
|
||||
* bswap16, 32, and 64 unconditionally swap byte order of integers of
|
||||
* the respective bitsize.
|
||||
*
|
||||
* The advantage of writing them out like this is that the bit
|
||||
* patterns are easily validated by inspection. Also, this form is
|
||||
* more likely to be picked up by the compiler and converted into
|
||||
* byte-swap machine instructions (if those exist) than something
|
||||
* loop-based.
|
||||
*/
|
||||
|
||||
uint16_t
|
||||
bswap16(uint16_t val)
|
||||
{
|
||||
return ((val & 0x00ff) << 8)
|
||||
| ((val & 0xff00) >> 8);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
bswap32(uint32_t val)
|
||||
{
|
||||
return ((val & 0x000000ff) << 24)
|
||||
| ((val & 0x0000ff00) << 8)
|
||||
| ((val & 0x00ff0000) >> 8)
|
||||
| ((val & 0xff000000) >> 24);
|
||||
}
|
||||
|
||||
uint64_t
|
||||
bswap64(uint64_t val)
|
||||
{
|
||||
return ((val & 0x00000000000000ff) << 56)
|
||||
| ((val & 0x000000000000ff00) << 40)
|
||||
| ((val & 0x0000000000ff0000) << 24)
|
||||
| ((val & 0x00000000ff000000) << 8)
|
||||
| ((val & 0x000000ff00000000) << 8)
|
||||
| ((val & 0x0000ff0000000000) << 24)
|
||||
| ((val & 0x00ff000000000000) >> 40)
|
||||
| ((val & 0xff00000000000000) >> 56);
|
||||
}
|
||||
|
||||
/*
|
||||
* Network byte order byte-swap functions.
|
||||
*
|
||||
* For ntoh* and hton*:
|
||||
* *s are for "short" (16-bit)
|
||||
* *l are for "long" (32-bit)
|
||||
* *ll are for "long long" (64-bit)
|
||||
*
|
||||
* hton* convert from host byte order to network byte order.
|
||||
* ntoh* convert from network byte order to host byte order.
|
||||
*
|
||||
* Network byte order is big-endian.
|
||||
*
|
||||
* Note that right now the only platforms OS/161 runs on are
|
||||
* big-endian, so these functions are actually all empty.
|
||||
*
|
||||
* These should maybe be made inline.
|
||||
*/
|
||||
|
||||
#if _BYTE_ORDER == _LITTLE_ENDIAN
|
||||
#define TO(tag, bits, type) \
|
||||
type ntoh##tag(type val) { return bswap##bits(val); } \
|
||||
type hton##tag(type val) { return bswap##bits(val); }
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Use a separate #if, so if the header file defining the symbols gets
|
||||
* omitted or messed up the build will fail instead of silently choosing
|
||||
* the wrong option.
|
||||
*/
|
||||
#if _BYTE_ORDER == _BIG_ENDIAN
|
||||
#define TO(tag, bits, type) \
|
||||
type ntoh##tag(type val) { return val; } \
|
||||
type hton##tag(type val) { return val; }
|
||||
#endif
|
||||
|
||||
#if _BYTE_ORDER == _PDP_ENDIAN
|
||||
#error "You lose."
|
||||
#endif
|
||||
|
||||
#ifndef TO
|
||||
#error "_BYTE_ORDER not set"
|
||||
#endif
|
||||
|
||||
TO(s, 16, uint16_t)
|
||||
TO(l, 32, uint32_t)
|
||||
TO(ll, 64, uint64_t)
|
||||
|
||||
|
||||
/*
|
||||
* Some utility functions for handling 64-bit values.
|
||||
*
|
||||
* join32to64 pastes two adjoining 32-bit values together in the right
|
||||
* way to treat them as a 64-bit value, depending on endianness.
|
||||
* split64to32 is the inverse operation.
|
||||
*
|
||||
* The 32-bit arguments should be passed in the order they appear in
|
||||
* memory, not as high word and low word; the whole point of these
|
||||
* functions is to know which is the high word and which is the low
|
||||
* word.
|
||||
*/
|
||||
|
||||
void
|
||||
join32to64(uint32_t x1, uint32_t x2, uint64_t *y2)
|
||||
{
|
||||
#if _BYTE_ORDER == _BIG_ENDIAN
|
||||
*y2 = ((uint64_t)x1 << 32) | (uint64_t)x2;
|
||||
#elif _BYTE_ORDER == _LITTLE_ENDIAN
|
||||
*y2 = (uint64_t)x1 | ((uint64_t)x2 << 32);
|
||||
#else
|
||||
#error "Eh?"
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
split64to32(uint64_t x, uint32_t *y1, uint32_t *y2)
|
||||
{
|
||||
#if _BYTE_ORDER == _BIG_ENDIAN
|
||||
*y1 = x >> 32;
|
||||
*y2 = x & 0xffffffff;
|
||||
#elif _BYTE_ORDER == _LITTLE_ENDIAN
|
||||
*y1 = x & 0xffffffff;
|
||||
*y2 = x >> 32;
|
||||
#else
|
||||
#error "Eh?"
|
||||
#endif
|
||||
}
|
113
kern/lib/kgets.c
Normal file
113
kern/lib/kgets.c
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright (c) 2001
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* 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 University 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 UNIVERSITY 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 UNIVERSITY 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 <types.h>
|
||||
#include <lib.h>
|
||||
|
||||
/*
|
||||
* Do a backspace in typed input.
|
||||
* We overwrite the current character with a space in case we're on
|
||||
* a terminal where backspace is nondestructive.
|
||||
*/
|
||||
static
|
||||
void
|
||||
backsp(void)
|
||||
{
|
||||
putch('\b');
|
||||
putch(' ');
|
||||
putch('\b');
|
||||
}
|
||||
|
||||
/*
|
||||
* Read a string off the console. Support a few of the more useful
|
||||
* common control characters. Do not include the terminating newline
|
||||
* in the buffer passed back.
|
||||
*/
|
||||
void
|
||||
kgets(char *buf, size_t maxlen)
|
||||
{
|
||||
size_t pos = 0;
|
||||
int ch;
|
||||
|
||||
while (1) {
|
||||
ch = getch();
|
||||
if (ch=='\n' || ch=='\r') {
|
||||
putch('\n');
|
||||
break;
|
||||
}
|
||||
|
||||
/* Only allow the normal 7-bit ascii */
|
||||
if (ch>=32 && ch<127 && pos < maxlen-1) {
|
||||
putch(ch);
|
||||
buf[pos++] = ch;
|
||||
}
|
||||
else if ((ch=='\b' || ch==127) && pos>0) {
|
||||
/* backspace */
|
||||
backsp();
|
||||
pos--;
|
||||
}
|
||||
else if (ch==3) {
|
||||
/* ^C - return empty string */
|
||||
putch('^');
|
||||
putch('C');
|
||||
putch('\n');
|
||||
pos = 0;
|
||||
break;
|
||||
}
|
||||
else if (ch==18) {
|
||||
/* ^R - reprint input */
|
||||
buf[pos] = 0;
|
||||
kprintf("^R\n%s", buf);
|
||||
}
|
||||
else if (ch==21) {
|
||||
/* ^U - erase line */
|
||||
while (pos > 0) {
|
||||
backsp();
|
||||
pos--;
|
||||
}
|
||||
}
|
||||
else if (ch==23) {
|
||||
/* ^W - erase word */
|
||||
while (pos > 0 && buf[pos-1]==' ') {
|
||||
backsp();
|
||||
pos--;
|
||||
}
|
||||
while (pos > 0 && buf[pos-1]!=' ') {
|
||||
backsp();
|
||||
pos--;
|
||||
}
|
||||
}
|
||||
else {
|
||||
beep();
|
||||
}
|
||||
}
|
||||
|
||||
buf[pos] = 0;
|
||||
}
|
216
kern/lib/kprintf.c
Normal file
216
kern/lib/kprintf.c
Normal file
@@ -0,0 +1,216 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* 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 University 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 UNIVERSITY 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 UNIVERSITY 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 <types.h>
|
||||
#include <kern/unistd.h>
|
||||
#include <stdarg.h>
|
||||
#include <lib.h>
|
||||
#include <spl.h>
|
||||
#include <cpu.h>
|
||||
#include <thread.h>
|
||||
#include <current.h>
|
||||
#include <synch.h>
|
||||
#include <mainbus.h>
|
||||
#include <vfs.h> // for vfs_sync()
|
||||
#include <lamebus/ltrace.h> // for ltrace_stop()
|
||||
|
||||
|
||||
/* Flags word for DEBUG() macro. */
|
||||
uint32_t dbflags = 0;
|
||||
|
||||
/* Lock for non-polled kprintfs */
|
||||
static struct lock *kprintf_lock;
|
||||
|
||||
/* Lock for polled kprintfs */
|
||||
static struct spinlock kprintf_spinlock;
|
||||
|
||||
|
||||
/*
|
||||
* Warning: all this has to work from interrupt handlers and when
|
||||
* interrupts are disabled.
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* Create the kprintf lock. Must be called before creating a second
|
||||
* thread or enabling a second CPU.
|
||||
*/
|
||||
void
|
||||
kprintf_bootstrap(void)
|
||||
{
|
||||
KASSERT(kprintf_lock == NULL);
|
||||
|
||||
kprintf_lock = lock_create("kprintf_lock");
|
||||
if (kprintf_lock == NULL) {
|
||||
panic("Could not create kprintf_lock\n");
|
||||
}
|
||||
spinlock_init(&kprintf_spinlock);
|
||||
}
|
||||
|
||||
/*
|
||||
* Send characters to the console. Backend for __printf.
|
||||
*/
|
||||
static
|
||||
void
|
||||
console_send(void *junk, const char *data, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
(void)junk;
|
||||
|
||||
for (i=0; i<len; i++) {
|
||||
putch(data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Printf to the console.
|
||||
*/
|
||||
int
|
||||
kprintf(const char *fmt, ...)
|
||||
{
|
||||
int chars;
|
||||
va_list ap;
|
||||
bool dolock;
|
||||
|
||||
dolock = kprintf_lock != NULL
|
||||
&& curthread->t_in_interrupt == false
|
||||
&& curthread->t_curspl == 0
|
||||
&& curcpu->c_spinlocks == 0;
|
||||
|
||||
if (dolock) {
|
||||
lock_acquire(kprintf_lock);
|
||||
}
|
||||
else {
|
||||
spinlock_acquire(&kprintf_spinlock);
|
||||
}
|
||||
|
||||
va_start(ap, fmt);
|
||||
chars = __vprintf(console_send, NULL, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
if (dolock) {
|
||||
lock_release(kprintf_lock);
|
||||
}
|
||||
else {
|
||||
spinlock_release(&kprintf_spinlock);
|
||||
}
|
||||
|
||||
return chars;
|
||||
}
|
||||
|
||||
/*
|
||||
* panic() is for fatal errors. It prints the printf arguments it's
|
||||
* passed and then halts the system.
|
||||
*/
|
||||
|
||||
void
|
||||
panic(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
/*
|
||||
* When we reach panic, the system is usually fairly screwed up.
|
||||
* It's not entirely uncommon for anything else we try to do
|
||||
* here to trigger more panics.
|
||||
*
|
||||
* This variable makes sure that if we try to do something here,
|
||||
* and it causes another panic, *that* panic doesn't try again;
|
||||
* trying again almost inevitably causes infinite recursion.
|
||||
*
|
||||
* This is not excessively paranoid - these things DO happen!
|
||||
*/
|
||||
static volatile int evil;
|
||||
|
||||
if (evil == 0) {
|
||||
evil = 1;
|
||||
|
||||
/*
|
||||
* Not only do we not want to be interrupted while
|
||||
* panicking, but we also want the console to be
|
||||
* printing in polling mode so as not to do context
|
||||
* switches. So turn interrupts off on this CPU.
|
||||
*/
|
||||
splhigh();
|
||||
}
|
||||
|
||||
if (evil == 1) {
|
||||
evil = 2;
|
||||
|
||||
/* Kill off other threads and halt other CPUs. */
|
||||
thread_panic();
|
||||
}
|
||||
|
||||
if (evil == 2) {
|
||||
evil = 3;
|
||||
|
||||
/* Print the message. */
|
||||
kprintf("panic: ");
|
||||
va_start(ap, fmt);
|
||||
__vprintf(console_send, NULL, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
if (evil == 3) {
|
||||
evil = 4;
|
||||
|
||||
/* Drop to the debugger. */
|
||||
ltrace_stop(0);
|
||||
}
|
||||
|
||||
if (evil == 4) {
|
||||
evil = 5;
|
||||
|
||||
/* Try to sync the disks. */
|
||||
vfs_sync();
|
||||
}
|
||||
|
||||
if (evil == 5) {
|
||||
evil = 6;
|
||||
|
||||
/* Shut down or reboot the system. */
|
||||
mainbus_panic();
|
||||
}
|
||||
|
||||
/*
|
||||
* Last resort, just in case.
|
||||
*/
|
||||
|
||||
for (;;);
|
||||
}
|
||||
|
||||
/*
|
||||
* Assertion failures go through this.
|
||||
*/
|
||||
void
|
||||
badassert(const char *expr, const char *file, int line, const char *func)
|
||||
{
|
||||
panic("Assertion failed: %s, at %s:%d (%s)\n",
|
||||
expr, file, line, func);
|
||||
}
|
62
kern/lib/misc.c
Normal file
62
kern/lib/misc.c
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* 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 University 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 UNIVERSITY 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 UNIVERSITY 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 <types.h>
|
||||
#include <kern/errmsg.h>
|
||||
#include <lib.h>
|
||||
|
||||
/*
|
||||
* Like strdup, but calls kmalloc.
|
||||
*/
|
||||
char *
|
||||
kstrdup(const char *s)
|
||||
{
|
||||
char *z;
|
||||
|
||||
z = kmalloc(strlen(s)+1);
|
||||
if (z == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
strcpy(z, s);
|
||||
return z;
|
||||
}
|
||||
|
||||
/*
|
||||
* Standard C function to return a string for a given errno.
|
||||
* Kernel version; panics if it hits an unknown error.
|
||||
*/
|
||||
const char *
|
||||
strerror(int errcode)
|
||||
{
|
||||
if (errcode>=0 && errcode < sys_nerr) {
|
||||
return sys_errlist[errcode];
|
||||
}
|
||||
panic("Invalid error code %d\n", errcode);
|
||||
return NULL;
|
||||
}
|
69
kern/lib/time.c
Normal file
69
kern/lib/time.c
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* 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 University 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 UNIVERSITY 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 UNIVERSITY 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 <types.h>
|
||||
#include <clock.h>
|
||||
|
||||
/*
|
||||
* ts1 + ts2
|
||||
*/
|
||||
void
|
||||
timespec_add(const struct timespec *ts1,
|
||||
const struct timespec *ts2,
|
||||
struct timespec *ret)
|
||||
{
|
||||
ret->tv_nsec = ts1->tv_nsec + ts2->tv_nsec;
|
||||
ret->tv_sec = ts1->tv_sec + ts2->tv_sec;
|
||||
if (ret->tv_nsec >= 1000000000) {
|
||||
ret->tv_nsec -= 1000000000;
|
||||
ret->tv_sec += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* ts1 - ts2
|
||||
*/
|
||||
void
|
||||
timespec_sub(const struct timespec *ts1,
|
||||
const struct timespec *ts2,
|
||||
struct timespec *ret)
|
||||
{
|
||||
/* in case ret and ts1 or ts2 are the same */
|
||||
struct timespec r;
|
||||
|
||||
r = *ts1;
|
||||
if (r.tv_nsec < ts2->tv_nsec) {
|
||||
r.tv_nsec += 1000000000;
|
||||
r.tv_sec--;
|
||||
}
|
||||
|
||||
r.tv_nsec -= ts2->tv_nsec;
|
||||
r.tv_sec -= ts2->tv_sec;
|
||||
*ret = r;
|
||||
}
|
164
kern/lib/uio.c
Normal file
164
kern/lib/uio.c
Normal file
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* 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 University 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 UNIVERSITY 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 UNIVERSITY 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 <types.h>
|
||||
#include <lib.h>
|
||||
#include <uio.h>
|
||||
#include <proc.h>
|
||||
#include <current.h>
|
||||
#include <copyinout.h>
|
||||
|
||||
/*
|
||||
* See uio.h for a description.
|
||||
*/
|
||||
|
||||
int
|
||||
uiomove(void *ptr, size_t n, struct uio *uio)
|
||||
{
|
||||
struct iovec *iov;
|
||||
size_t size;
|
||||
int result;
|
||||
|
||||
if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE) {
|
||||
panic("uiomove: Invalid uio_rw %d\n", (int) uio->uio_rw);
|
||||
}
|
||||
if (uio->uio_segflg==UIO_SYSSPACE) {
|
||||
KASSERT(uio->uio_space == NULL);
|
||||
}
|
||||
else {
|
||||
KASSERT(uio->uio_space == proc_getas());
|
||||
}
|
||||
|
||||
while (n > 0 && uio->uio_resid > 0) {
|
||||
/* get the first iovec */
|
||||
iov = uio->uio_iov;
|
||||
size = iov->iov_len;
|
||||
|
||||
if (size > n) {
|
||||
size = n;
|
||||
}
|
||||
|
||||
if (size == 0) {
|
||||
/* move to the next iovec and try again */
|
||||
uio->uio_iov++;
|
||||
uio->uio_iovcnt--;
|
||||
if (uio->uio_iovcnt == 0) {
|
||||
/*
|
||||
* This should only happen if you set
|
||||
* uio_resid incorrectly (to more than
|
||||
* the total length of buffers the uio
|
||||
* points to).
|
||||
*/
|
||||
panic("uiomove: ran out of buffers\n");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (uio->uio_segflg) {
|
||||
case UIO_SYSSPACE:
|
||||
if (uio->uio_rw == UIO_READ) {
|
||||
memmove(iov->iov_kbase, ptr, size);
|
||||
}
|
||||
else {
|
||||
memmove(ptr, iov->iov_kbase, size);
|
||||
}
|
||||
iov->iov_kbase = ((char *)iov->iov_kbase+size);
|
||||
break;
|
||||
case UIO_USERSPACE:
|
||||
case UIO_USERISPACE:
|
||||
if (uio->uio_rw == UIO_READ) {
|
||||
result = copyout(ptr, iov->iov_ubase,size);
|
||||
}
|
||||
else {
|
||||
result = copyin(iov->iov_ubase, ptr, size);
|
||||
}
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
iov->iov_ubase += size;
|
||||
break;
|
||||
default:
|
||||
panic("uiomove: Invalid uio_segflg %d\n",
|
||||
(int)uio->uio_segflg);
|
||||
}
|
||||
|
||||
iov->iov_len -= size;
|
||||
uio->uio_resid -= size;
|
||||
uio->uio_offset += size;
|
||||
ptr = ((char *)ptr + size);
|
||||
n -= size;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
uiomovezeros(size_t n, struct uio *uio)
|
||||
{
|
||||
/* static, so initialized as zero */
|
||||
static char zeros[16];
|
||||
size_t amt;
|
||||
int result;
|
||||
|
||||
/* This only makes sense when reading */
|
||||
KASSERT(uio->uio_rw == UIO_READ);
|
||||
|
||||
while (n > 0) {
|
||||
amt = sizeof(zeros);
|
||||
if (amt > n) {
|
||||
amt = n;
|
||||
}
|
||||
result = uiomove(zeros, amt, uio);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
n -= amt;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Convenience function to initialize an iovec and uio for kernel I/O.
|
||||
*/
|
||||
|
||||
void
|
||||
uio_kinit(struct iovec *iov, struct uio *u,
|
||||
void *kbuf, size_t len, off_t pos, enum uio_rw rw)
|
||||
{
|
||||
iov->iov_kbase = kbuf;
|
||||
iov->iov_len = len;
|
||||
u->uio_iov = iov;
|
||||
u->uio_iovcnt = 1;
|
||||
u->uio_offset = pos;
|
||||
u->uio_resid = len;
|
||||
u->uio_segflg = UIO_SYSSPACE;
|
||||
u->uio_rw = rw;
|
||||
u->uio_space = NULL;
|
||||
}
|
Reference in New Issue
Block a user