| 1 | /* | 
|---|
| 2 | * kdmsg.c - output kernel debug/trace/information to an available terminal | 
|---|
| 3 | * (see kern/kdmsg.h) | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless | 
|---|
| 6 | * Copyright (c) 2011,2012 UPMC Sorbonne Universites | 
|---|
| 7 | * | 
|---|
| 8 | * This file is part of ALMOS-kernel. | 
|---|
| 9 | * | 
|---|
| 10 | * ALMOS-kernel is free software; you can redistribute it and/or modify it | 
|---|
| 11 | * under the terms of the GNU General Public License as published by | 
|---|
| 12 | * the Free Software Foundation; version 2.0 of the License. | 
|---|
| 13 | * | 
|---|
| 14 | * ALMOS-kernel is distributed in the hope that it will be useful, but | 
|---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 17 | * General Public License for more details. | 
|---|
| 18 | * | 
|---|
| 19 | * You should have received a copy of the GNU General Public License | 
|---|
| 20 | * along with ALMOS-kernel; if not, write to the Free Software Foundation, | 
|---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 
|---|
| 22 | */ | 
|---|
| 23 |  | 
|---|
| 24 | #include <types.h> | 
|---|
| 25 | #include <hardware.h> | 
|---|
| 26 | #include <cpu.h> | 
|---|
| 27 | #include <chdev.h> | 
|---|
| 28 | #include <tty.h> | 
|---|
| 29 | #include <libk.h> | 
|---|
| 30 | #include <kdmsg.h> | 
|---|
| 31 | #include <spinlock.h> | 
|---|
| 32 |  | 
|---|
| 33 | spinlock_t exception_lock; | 
|---|
| 34 | spinlock_t printk_lock; | 
|---|
| 35 | spinlock_t isr_lock; | 
|---|
| 36 |  | 
|---|
| 37 | void kdmsg_init() | 
|---|
| 38 | { | 
|---|
| 39 | spinlock_init(&exception_lock); | 
|---|
| 40 | spinlock_init(&printk_lock); | 
|---|
| 41 | spinlock_init(&isr_lock); | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | int __fprintk (int tty, spinlock_t *lock, const char *fmt, ...) | 
|---|
| 45 | { | 
|---|
| 46 | va_list ap; | 
|---|
| 47 | int count; | 
|---|
| 48 | char buff[256]; | 
|---|
| 49 | dev_request_t rq; | 
|---|
| 50 |  | 
|---|
| 51 | va_start (ap, fmt); | 
|---|
| 52 |  | 
|---|
| 53 | assert(tty == 0 && "Unexpected tty device id"); | 
|---|
| 54 | count = iprintk (buff, 1, (char *) fmt, ap); | 
|---|
| 55 | assert(count < 256 && "Buffer overflow"); | 
|---|
| 56 |  | 
|---|
| 57 | va_end (ap); | 
|---|
| 58 | rq.src   = buff; | 
|---|
| 59 | rq.count = count; | 
|---|
| 60 | rq.flags = DEV_RQ_NOBLOCK; | 
|---|
| 61 |  | 
|---|
| 62 | if(lock) | 
|---|
| 63 | { | 
|---|
| 64 | if(lock == &printk_lock) | 
|---|
| 65 | spinlock_lock(&ttys_tbl[tty].lock); | 
|---|
| 66 | else | 
|---|
| 67 | cpu_spinlock_lock(&ttys_tbl[tty].lock.val); | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | ttys_tbl[tty].op.dev.write(&ttys_tbl[tty], &rq); | 
|---|
| 71 |  | 
|---|
| 72 | if(lock) | 
|---|
| 73 | { | 
|---|
| 74 | if(lock == &printk_lock) | 
|---|
| 75 | spinlock_unlock(&ttys_tbl[tty].lock); | 
|---|
| 76 | else | 
|---|
| 77 | cpu_spinlock_unlock(&ttys_tbl[tty].lock.val); | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | return count; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | int __perror (int fatal, const char *fmt, ...) | 
|---|
| 84 | { | 
|---|
| 85 | va_list ap; | 
|---|
| 86 | int count; | 
|---|
| 87 | char buff[256]; | 
|---|
| 88 | dev_request_t rq; | 
|---|
| 89 |  | 
|---|
| 90 | va_start (ap, fmt); | 
|---|
| 91 | count = iprintk (buff, 1, (char *) fmt, ap); | 
|---|
| 92 | va_end (ap); | 
|---|
| 93 |  | 
|---|
| 94 | rq.src = buff; | 
|---|
| 95 | rq.count = count; | 
|---|
| 96 | rq.flags = DEV_RQ_NOBLOCK; | 
|---|
| 97 |  | 
|---|
| 98 | ttys_tbl[0].op.dev.write(&ttys_tbl[0], &rq); | 
|---|
| 99 |  | 
|---|
| 100 | if(fatal) | 
|---|
| 101 | while(1); | 
|---|
| 102 |  | 
|---|
| 103 | return count; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | void bdump(uint_t fd, char* buff, size_t count) | 
|---|
| 107 | { | 
|---|
| 108 | register uint_t i,j,size; | 
|---|
| 109 | char *tab = "0123456789ABCDEF"; | 
|---|
| 110 | char tmp[256]; | 
|---|
| 111 | dev_request_t rq; | 
|---|
| 112 |  | 
|---|
| 113 | assert((fd != 0) && "file descriptor is not a tty"); | 
|---|
| 114 | i=0; | 
|---|
| 115 |  | 
|---|
| 116 | while(count) | 
|---|
| 117 | { | 
|---|
| 118 | size = (count < 256) ? count : 256; | 
|---|
| 119 |  | 
|---|
| 120 | for(j=0; j < size; j++, i++) | 
|---|
| 121 | tmp[j] = tab[ (buff[i] & 0xF)]; | 
|---|
| 122 |  | 
|---|
| 123 | rq.src   = tmp; | 
|---|
| 124 | rq.count = 256; | 
|---|
| 125 | rq.flags = 0; | 
|---|
| 126 |  | 
|---|
| 127 | ttys_tbl[0].op.dev.write(&ttys_tbl[0], &rq); | 
|---|
| 128 |  | 
|---|
| 129 | count -= size; | 
|---|
| 130 | } | 
|---|
| 131 | } | 
|---|