Subversion Repositories HelenOS-historic

Rev

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

  1. /*
  2.  * Copyright (C) 2001-2004 Jakub Jermar
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. #include <func.h>
  30. #include <print.h>
  31. #include <cpu.h>
  32. #include <arch/asm.h>
  33. #include <arch.h>
  34. #include <typedefs.h>
  35. #include <console/kconsole.h>
  36.  
  37. atomic_t haltstate = {0}; /**< Halt flag */
  38.  
  39.  
  40. /** Halt wrapper
  41.  *
  42.  * Set halt flag and halt the cpu.
  43.  *
  44.  */
  45. void halt()
  46. {
  47. #ifdef CONFIG_DEBUG
  48.     bool rundebugger = false;
  49.  
  50. //      TODO test_and_set not defined on all arches
  51. //  if (!test_and_set(&haltstate))
  52.     if (!atomic_get(&haltstate)) {
  53.         atomic_set(&haltstate, 1);
  54.         rundebugger = true;
  55.     }
  56. #else
  57.     atomic_set(&haltstate, 1);
  58. #endif
  59.  
  60.     interrupts_disable();
  61. #ifdef CONFIG_DEBUG
  62.     if (rundebugger) {
  63.         printf("\n");
  64.         kconsole("panic"); /* Run kconsole as a last resort to user */
  65.     }
  66. #endif      
  67.     if (CPU)
  68.         printf("cpu%d: halted\n", CPU->id);
  69.     else
  70.         printf("cpu: halted\n");
  71.     cpu_halt();
  72. }
  73.  
  74. /** Return number of characters in a string.
  75.  *
  76.  * @param str NULL terminated string.
  77.  *
  78.  * @return Number of characters in str.
  79.  */
  80. size_t strlen(const char *str)
  81. {
  82.     int i;
  83.    
  84.     for (i = 0; str[i]; i++)
  85.         ;
  86.    
  87.     return i;
  88. }
  89.  
  90. /** Compare two NULL terminated strings
  91.  *
  92.  * Do a char-by-char comparison of two NULL terminated strings.
  93.  * The strings are considered equal iff they consist of the same
  94.  * characters on the minimum of their lengths and specified maximal
  95.  * length.
  96.  *
  97.  * @param src First string to compare.
  98.  * @param dst Second string to compare.
  99.  * @param len Maximal length for comparison.
  100.  *
  101.  * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
  102.  *
  103.  */
  104. int strncmp(const char *src, const char *dst, size_t len)
  105. {
  106.     int i;
  107.    
  108.     i = 0;
  109.     for (;*src && *dst && i < len;src++,dst++,i++) {
  110.         if (*src < *dst)
  111.             return -1;
  112.         if (*src > *dst)
  113.             return 1;
  114.     }
  115.     if (i == len || *src == *dst)
  116.         return 0;
  117.     if (!*src)
  118.         return -1;
  119.     return 1;
  120. }
  121.  
  122. /** Copy NULL terminated string.
  123.  *
  124.  * Copy at most 'len' characters from string 'src' to 'dest'.
  125.  * If 'src' is shorter than 'len', '\0' is inserted behind the
  126.  * last copied character.
  127.  *
  128.  * @param src Source string.
  129.  * @param dst Destination buffer.
  130.  * @param len Size of destination buffer.
  131.  */
  132. void strncpy(char *dest, const char *src, size_t len)
  133. {
  134.     int i;
  135.     for (i = 0; i < len; i++) {
  136.         if (!(dest[i] = src[i]))
  137.             return;
  138.     }
  139.     dest[i-1] = '\0';
  140. }
  141.  
  142. /** Convert ascii representation to __native
  143.  *
  144.  * Supports 0x for hexa & 0 for octal notation.
  145.  * Does not check for overflows, does not support negative numbers
  146.  *
  147.  * @param text Textual representation of number
  148.  * @return Converted number or 0 if no valid number ofund
  149.  */
  150. __native atoi(const char *text)
  151. {
  152.     int base = 10;
  153.     __native result = 0;
  154.  
  155.     if (text[0] == '0' && text[1] == 'x') {
  156.         base = 16;
  157.         text += 2;
  158.     } else if (text[0] == '0')
  159.         base = 8;
  160.  
  161.     while (*text) {
  162.         if (base != 16 && \
  163.             ((*text >= 'A' && *text <= 'F' )
  164.              || (*text >='a' && *text <='f')))
  165.             break;
  166.         if (base == 8 && *text >='8')
  167.             break;
  168.  
  169.         if (*text >= '0' && *text <= '9') {
  170.             result *= base;
  171.             result += *text - '0';
  172.         } else if (*text >= 'A' && *text <= 'F') {
  173.             result *= base;
  174.             result += *text - 'A' + 10;
  175.         } else if (*text >= 'a' && *text <= 'f') {
  176.             result *= base;
  177.             result += *text - 'a' + 10;
  178.         } else
  179.             break;
  180.         text++;
  181.     }
  182.  
  183.     return result;
  184. }
  185.