Subversion Repositories HelenOS-historic

Rev

Rev 635 | Rev 1264 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 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>
517 jermar 34
#include <typedefs.h>
607 palkovsky 35
#include <console/kconsole.h>
1 jermar 36
 
631 palkovsky 37
atomic_t haltstate = {0}; /**< Halt flag */
1 jermar 38
 
62 decky 39
 
40
/** Halt wrapper
41
 *
42
 * Set halt flag and halt the cpu.
43
 *
44
 */
631 palkovsky 45
void halt()
1 jermar 46
{
631 palkovsky 47
#ifdef CONFIG_DEBUG
659 jermar 48
    bool rundebugger = false;
631 palkovsky 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
 
413 jermar 60
    interrupts_disable();
607 palkovsky 61
#ifdef CONFIG_DEBUG
631 palkovsky 62
    if (rundebugger) {
63
        printf("\n");
64
        kconsole("panic"); /* Run kconsole as a last resort to user */
65
    }
607 palkovsky 66
#endif      
227 jermar 67
    if (CPU)
68
        printf("cpu%d: halted\n", CPU->id);
69
    else
70
        printf("cpu: halted\n");
1 jermar 71
    cpu_halt();
72
}
73
 
517 jermar 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
}
1 jermar 89
 
62 decky 90
/** Compare two NULL terminated strings
91
 *
517 jermar 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.
62 decky 96
 *
97
 * @param src First string to compare.
98
 * @param dst Second string to compare.
517 jermar 99
 * @param len Maximal length for comparison.
62 decky 100
 *
601 palkovsky 101
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
62 decky 102
 *
1 jermar 103
 */
518 jermar 104
int strncmp(const char *src, const char *dst, size_t len)
1 jermar 105
{
106
    int i;
107
 
108
    i = 0;
601 palkovsky 109
    for (;*src && *dst && i < len;src++,dst++,i++) {
110
        if (*src < *dst)
111
            return -1;
112
        if (*src > *dst)
113
            return 1;
1 jermar 114
    }
601 palkovsky 115
    if (i == len || *src == *dst)
116
        return 0;
635 palkovsky 117
    if (!*src)
601 palkovsky 118
        return -1;
1 jermar 119
    return 1;
120
}
331 bondari 121
 
518 jermar 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
    }
585 palkovsky 139
    dest[i-1] = '\0';
518 jermar 140
}
582 palkovsky 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) {
603 palkovsky 162
        if (base != 16 && \
163
            ((*text >= 'A' && *text <= 'F' )
164
             || (*text >='a' && *text <='f')))
582 palkovsky 165
            break;
166
        if (base == 8 && *text >='8')
167
            break;
168
 
591 palkovsky 169
        if (*text >= '0' && *text <= '9') {
170
            result *= base;
582 palkovsky 171
            result += *text - '0';
591 palkovsky 172
        } else if (*text >= 'A' && *text <= 'F') {
173
            result *= base;
582 palkovsky 174
            result += *text - 'A' + 10;
603 palkovsky 175
        } else if (*text >= 'a' && *text <= 'f') {
176
            result *= base;
177
            result += *text - 'a' + 10;
591 palkovsky 178
        } else
582 palkovsky 179
            break;
180
        text++;
181
    }
182
 
183
    return result;
184
}