Subversion Repositories HelenOS

Rev

Rev 4344 | Rev 4347 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2003 Josef Cejka
  3.  * Copyright (c) 2005 Jakub Jermar
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  * - Redistributions in binary form must reproduce the above copyright
  13.  *   notice, this list of conditions and the following disclaimer in the
  14.  *   documentation and/or other materials provided with the distribution.
  15.  * - The name of the author may not be used to endorse or promote products
  16.  *   derived from this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  */
  29.  
  30. /** @addtogroup genericconsole
  31.  * @{
  32.  */
  33. /** @file
  34.  */
  35.  
  36. #include <console/console.h>
  37. #include <console/chardev.h>
  38. #include <sysinfo/sysinfo.h>
  39. #include <synch/waitq.h>
  40. #include <synch/spinlock.h>
  41. #include <arch/types.h>
  42. #include <ddi/device.h>
  43. #include <ddi/irq.h>
  44. #include <ddi/ddi.h>
  45. #include <ipc/irq.h>
  46. #include <arch.h>
  47. #include <func.h>
  48. #include <print.h>
  49. #include <atomic.h>
  50.  
  51. #define KLOG_SIZE PAGE_SIZE
  52. #define KLOG_LATENCY 8
  53.  
  54. /** Kernel log cyclic buffer */
  55. static char klog[KLOG_SIZE] __attribute__ ((aligned (PAGE_SIZE)));
  56.  
  57. /** Kernel log initialized */
  58. static bool klog_inited = false;
  59. /** First kernel log characters */
  60. static index_t klog_start = 0;
  61. /** Number of valid kernel log characters */
  62. static size_t klog_len = 0;
  63. /** Number of stored (not printed) kernel log characters */
  64. static size_t klog_stored = 0;
  65. /** Number of stored kernel log characters for uspace */
  66. static size_t klog_uspace = 0;
  67.  
  68. /** Silence output */
  69. bool silent = false;
  70.  
  71. /** Kernel log spinlock */
  72. SPINLOCK_INITIALIZE(klog_lock);
  73.  
  74. /** Physical memory area used for klog buffer */
  75. static parea_t klog_parea;
  76.  
  77. /** Standard input and output character devices */
  78. indev_t *stdin = NULL;
  79. outdev_t *stdout = NULL;
  80.  
  81. /** Initialize kernel logging facility
  82.  *
  83.  * The shared area contains kernel cyclic buffer. Userspace application may
  84.  * be notified on new data with indication of position and size
  85.  * of the data within the circular buffer.
  86.  *
  87.  */
  88. void klog_init(void)
  89. {
  90.     void *faddr = (void *) KA2PA(klog);
  91.    
  92.     ASSERT((uintptr_t) faddr % FRAME_SIZE == 0);
  93.     ASSERT(KLOG_SIZE % FRAME_SIZE == 0);
  94.    
  95.     klog_parea.pbase = (uintptr_t) faddr;
  96.     klog_parea.frames = SIZE2FRAMES(KLOG_SIZE);
  97.     ddi_parea_register(&klog_parea);
  98.    
  99.     sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr);
  100.     sysinfo_set_item_val("klog.pages", NULL, SIZE2FRAMES(KLOG_SIZE));
  101.    
  102.     //irq_initialize(&klog_irq);
  103.     //klog_irq.devno = devno;
  104.     //klog_irq.inr = KLOG_VIRT_INR;
  105.     //klog_irq.claim = klog_claim;
  106.     //irq_register(&klog_irq);
  107.    
  108.     spinlock_lock(&klog_lock);
  109.     klog_inited = true;
  110.     spinlock_unlock(&klog_lock);
  111. }
  112.  
  113. void grab_console(void)
  114. {
  115.     silent = false;
  116.     arch_grab_console();
  117. }
  118.  
  119. void release_console(void)
  120. {
  121.     silent = true;
  122.     arch_release_console();
  123. }
  124.  
  125. bool check_poll(indev_t *indev)
  126. {
  127.     if (indev == NULL)
  128.         return false;
  129.    
  130.     if (indev->op == NULL)
  131.         return false;
  132.    
  133.     return (indev->op->poll != NULL);
  134. }
  135.  
  136. /** Get character from input character device. Do not echo character.
  137.  *
  138.  * @param indev Input character device.
  139.  * @return Character read.
  140.  *
  141.  */
  142. uint8_t _getc(indev_t *indev)
  143. {
  144.     if (atomic_get(&haltstate)) {
  145.         /* If we are here, we are hopefully on the processor that
  146.          * issued the 'halt' command, so proceed to read the character
  147.          * directly from input
  148.          */
  149.         if (check_poll(indev))
  150.             return indev->op->poll(indev);
  151.        
  152.         /* No other way of interacting with user */
  153.         interrupts_disable();
  154.        
  155.         if (CPU)
  156.             printf("cpu%u: ", CPU->id);
  157.         else
  158.             printf("cpu: ");
  159.         printf("halted (no polling input)\n");
  160.         cpu_halt();
  161.     }
  162.    
  163.     waitq_sleep(&indev->wq);
  164.     ipl_t ipl = interrupts_disable();
  165.     spinlock_lock(&indev->lock);
  166.     uint8_t ch = indev->buffer[(indev->index - indev->counter) % INDEV_BUFLEN];
  167.     indev->counter--;
  168.     spinlock_unlock(&indev->lock);
  169.     interrupts_restore(ipl);
  170.    
  171.     return ch;
  172. }
  173.  
  174. /** Get string from input character device.
  175.  *
  176.  * Read characters from input character device until first occurrence
  177.  * of newline character.
  178.  *
  179.  * @param indev  Input character device.
  180.  * @param buf    Buffer where to store string terminated by '\0'.
  181.  * @param buflen Size of the buffer.
  182.  *
  183.  * @return Number of characters read.
  184.  *
  185.  */
  186. count_t gets(indev_t *indev, char *buf, size_t buflen)
  187. {
  188.     index_t index = 0;
  189.    
  190.     while (index < buflen) {
  191.         char ch = _getc(indev);
  192.         if (ch == '\b') {
  193.             if (index > 0) {
  194.                 index--;
  195.                 /* Space backspace, space */
  196.                 putchar('\b');
  197.                 putchar(' ');
  198.                 putchar('\b');
  199.             }
  200.             continue;
  201.         }
  202.         putchar(ch);
  203.        
  204.         if (ch == '\n') { /* end of string => write 0, return */
  205.             buf[index] = '\0';
  206.             return (count_t) index;
  207.         }
  208.         buf[index++] = ch;
  209.     }
  210.    
  211.     return (count_t) index;
  212. }
  213.  
  214. /** Get character from input device & echo it to screen */
  215. uint8_t getc(indev_t *indev)
  216. {
  217.     uint8_t ch = _getc(indev);
  218.     putchar(ch);
  219.     return ch;
  220. }
  221.  
  222. void klog_update(void)
  223. {
  224.     spinlock_lock(&klog_lock);
  225.    
  226. //  if ((klog_inited) && (klog_irq.notif_cfg.notify) && (klog_uspace > 0)) {
  227. //      ipc_irq_send_msg_3(&klog_irq, klog_start, klog_len, klog_uspace);
  228. //      klog_uspace = 0;
  229. //  }
  230.    
  231.     spinlock_unlock(&klog_lock);
  232. }
  233.  
  234. void putchar(char c)
  235. {
  236.     spinlock_lock(&klog_lock);
  237.    
  238.     if ((klog_stored > 0) && (stdout) && (stdout->op->write)) {
  239.         /* Print charaters stored in kernel log */
  240.         index_t i;
  241.         for (i = klog_len - klog_stored; i < klog_len; i++)
  242.             stdout->op->write(stdout, klog[(klog_start + i) % KLOG_SIZE], silent);
  243.         klog_stored = 0;
  244.     }
  245.    
  246.     /* Store character in the cyclic kernel log */
  247.     klog[(klog_start + klog_len) % KLOG_SIZE] = c;
  248.     if (klog_len < KLOG_SIZE)
  249.         klog_len++;
  250.     else
  251.         klog_start = (klog_start + 1) % KLOG_SIZE;
  252.    
  253.     if ((stdout) && (stdout->op->write))
  254.         stdout->op->write(stdout, c, silent);
  255.     else {
  256.         /* The character is just in the kernel log */
  257.         if (klog_stored < klog_len)
  258.             klog_stored++;
  259.     }
  260.    
  261.     /* The character is stored for uspace */
  262.     if (klog_uspace < klog_len)
  263.         klog_uspace++;
  264.    
  265.     /* Check notify uspace to update */
  266.     bool update;
  267.     if ((klog_uspace > KLOG_LATENCY) || (c == '\n'))
  268.         update = true;
  269.     else
  270.         update = false;
  271.    
  272.     spinlock_unlock(&klog_lock);
  273.    
  274.     if (update)
  275.         klog_update();
  276. }
  277.  
  278. /** @}
  279.  */
  280.