Various fixes and improvements.
- fixed bug that caused IDLE to panic (irq hook inconsistency); - kprintf() now accepts multiple arguments; moved to utility.c; - prepare_shutdown() signals system processes with SIGKSTOP; - phys_fill() renamed to phys_memset(), argument order changed; - kmemset() removed in favor of phys_kmemset(); - kstrncpy() removed in favor of phys_copy(); - katoi, kstrncmp replaced by normal library procedure again; - rm_irq_handler() interface changed (simply pass hook pointer);
This commit is contained in:
125
kernel/utility.c
125
kernel/utility.c
@@ -1,34 +1,143 @@
|
||||
/* This file contains a collection of miscellaneous procedures:
|
||||
* panic abort MINIX due to a fatal error
|
||||
* panic: abort MINIX due to a fatal error
|
||||
* kprintf: diagnostic output for the kernel
|
||||
*
|
||||
* Changes:
|
||||
* simple printing to circular buffer (Jorrit N. Herder)
|
||||
*
|
||||
* This file contains the routines that take care of kernel messages, i.e.,
|
||||
* diagnostic output within the kernel. Kernel messages are not directly
|
||||
* displayed on the console, because this must be done by the PRINT driver.
|
||||
* Instead, the kernel accumulates characters in a buffer and notifies the
|
||||
* output driver when a new message is ready.
|
||||
*/
|
||||
|
||||
#include "kernel.h"
|
||||
#include "assert.h"
|
||||
#include <unistd.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <minix/com.h>
|
||||
|
||||
#define END_OF_KMESS -1
|
||||
FORWARD _PROTOTYPE(void kputc, (int c));
|
||||
|
||||
|
||||
/*===========================================================================*
|
||||
* panic *
|
||||
*===========================================================================*/
|
||||
PUBLIC void panic(s,n)
|
||||
_CONST char *s;
|
||||
int n;
|
||||
PUBLIC void panic(mess,nr)
|
||||
_CONST char *mess;
|
||||
int nr;
|
||||
{
|
||||
/* The system has run aground of a fatal kernel error. Terminate execution. */
|
||||
static int panicking = 0;
|
||||
if (panicking ++) return; /* prevent recursive panics */
|
||||
|
||||
if (s != NULL) {
|
||||
kprintf("\nKernel panic: %s", karg(s));
|
||||
if (n != NO_NUM) kprintf(" %d", n);
|
||||
if (mess != NULL) {
|
||||
kprintf("\nKernel panic: %s", mess);
|
||||
if (nr != NO_NUM) kprintf(" %d", nr);
|
||||
kprintf("\n",NO_NUM);
|
||||
}
|
||||
prepare_shutdown(RBT_PANIC);
|
||||
}
|
||||
|
||||
|
||||
/*===========================================================================*
|
||||
* kprintf *
|
||||
*===========================================================================*/
|
||||
PUBLIC void kprintf(const char *fmt, ...) /* format to be printed */
|
||||
{
|
||||
int c; /* next character in fmt */
|
||||
unsigned long u; /* hold number argument */
|
||||
int base; /* base of number arg */
|
||||
int negative = 0; /* print minus sign */
|
||||
static char x2c[] = "0123456789ABCDEF"; /* nr conversion table */
|
||||
char ascii[8 * sizeof(long) / 3 + 2]; /* string for ascii number */
|
||||
char *s = NULL; /* string to be printed */
|
||||
va_list argp; /* optional arguments */
|
||||
|
||||
va_start(argp, fmt); /* init variable arguments */
|
||||
|
||||
while((c=*fmt++) != 0) {
|
||||
|
||||
if (c == '%') { /* expect format '%key' */
|
||||
switch(c = *fmt++) { /* determine what to do */
|
||||
|
||||
/* Known keys are %d, %u, %x, %s, and %%. This is easily extended
|
||||
* with number types like %b and %o by providing a different base.
|
||||
* Number type keys don't set a string to 's', but use the general
|
||||
* conversion after the switch statement.
|
||||
*/
|
||||
case 'd': /* output decimal */
|
||||
u = va_arg(argp, int);
|
||||
if (u < 0) { negative = 1; u = -u; }
|
||||
base = 10;
|
||||
break;
|
||||
case 'u': /* output unsigned long */
|
||||
u = va_arg(argp, unsigned long);
|
||||
base = 10;
|
||||
break;
|
||||
case 'x': /* output hexadecimal */
|
||||
u = va_arg(argp, unsigned long);
|
||||
base = 0x10;
|
||||
break;
|
||||
case 's': /* output string */
|
||||
s = va_arg(argp, char *);
|
||||
if (s == NULL) s = "(null)";
|
||||
break;
|
||||
case '%': /* output percent */
|
||||
s = "%";
|
||||
break;
|
||||
|
||||
/* Unrecognized key. */
|
||||
default: /* echo back %key */
|
||||
s = "%?";
|
||||
s[1] = c; /* set unknown key */
|
||||
}
|
||||
|
||||
/* Assume a number if no string is set. Convert to ascii. */
|
||||
if (s == NULL) {
|
||||
s = ascii + sizeof(ascii)-1;
|
||||
*s = 0;
|
||||
do { *--s = x2c[(u % base)]; } /* work backwards */
|
||||
while ((u /= base) > 0);
|
||||
}
|
||||
|
||||
/* This is where the actual output for format "%key" is done. */
|
||||
if (negative) kputc('-'); /* print sign if negative */
|
||||
while(*s != 0) { kputc(*s++); } /* print string/ number */
|
||||
}
|
||||
else {
|
||||
kputc(c); /* print and continue */
|
||||
}
|
||||
}
|
||||
kputc(END_OF_KMESS); /* terminate output */
|
||||
va_end(argp); /* end variable arguments */
|
||||
}
|
||||
|
||||
|
||||
/*===========================================================================*
|
||||
* kputc *
|
||||
*===========================================================================*/
|
||||
PRIVATE void kputc(c)
|
||||
int c; /* character to append */
|
||||
{
|
||||
/* Accumulate a single character for a kernel message. Send a notification
|
||||
* the to PRINTF_PROC driver if an END_OF_KMESS is encountered.
|
||||
*/
|
||||
if (c != END_OF_KMESS) {
|
||||
kmess.km_buf[kmess.km_next] = c; /* put normal char in buffer */
|
||||
if (kmess.km_size < KMESS_BUF_SIZE)
|
||||
kmess.km_size += 1;
|
||||
kmess.km_next = (kmess.km_next + 1) % KMESS_BUF_SIZE;
|
||||
} else {
|
||||
send_sig(PRINTF_PROC, SIGKMESS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#if TEMP_CODE
|
||||
|
||||
|
||||
Reference in New Issue
Block a user