Subversion Repositories HelenOS-historic

Rev

Rev 631 | Rev 659 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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