Changeset a35b458 in mainline for kernel/generic/src/lib
- Timestamp:
- 2018-03-02T20:10:49Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f1380b7
- Parents:
- 3061bc1
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:38:31)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:10:49)
- Location:
- kernel/generic/src/lib
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/lib/elf.c
r3061bc1 ra35b458 80 80 (header->e_ident[EI_MAG3] != ELFMAG3)) 81 81 return EE_INVALID; 82 82 83 83 /* Identify ELF compatibility */ 84 84 if ((header->e_ident[EI_DATA] != ELF_DATA_ENCODING) || … … 88 88 (header->e_ident[EI_CLASS] != ELF_CLASS)) 89 89 return EE_INCOMPATIBLE; 90 90 91 91 if (header->e_phentsize != sizeof(elf_segment_header_t)) 92 92 return EE_INCOMPATIBLE; 93 93 94 94 if (header->e_shentsize != sizeof(elf_section_header_t)) 95 95 return EE_INCOMPATIBLE; 96 96 97 97 /* Check if the object type is supported. */ 98 98 if (header->e_type != ET_EXEC) 99 99 return EE_UNSUPPORTED; 100 100 101 101 /* Check if the ELF image starts on a page boundary */ 102 102 if (ALIGN_UP((uintptr_t) header, PAGE_SIZE) != (uintptr_t) header) 103 103 return EE_UNSUPPORTED; 104 104 105 105 /* Walk through all segment headers and process them. */ 106 106 elf_half i; … … 109 109 &((elf_segment_header_t *)(((uint8_t *) header) + 110 110 header->e_phoff))[i]; 111 111 112 112 int rc = segment_header(seghdr, header, as, flags); 113 113 if (rc != EE_OK) 114 114 return rc; 115 115 } 116 116 117 117 /* Inspect all section headers and process them. */ 118 118 for (i = 0; i < header->e_shnum; i++) { … … 120 120 &((elf_section_header_t *)(((uint8_t *) header) + 121 121 header->e_shoff))[i]; 122 122 123 123 int rc = section_header(sechdr, header, as); 124 124 if (rc != EE_OK) 125 125 return rc; 126 126 } 127 127 128 128 return EE_OK; 129 129 } … … 139 139 { 140 140 assert(rc < sizeof(error_codes) / sizeof(char *)); 141 141 142 142 return error_codes[rc]; 143 143 } … … 199 199 backend_data.elf = elf; 200 200 backend_data.segment = entry; 201 201 202 202 if (entry->p_align > 1) { 203 203 if ((entry->p_offset % entry->p_align) != … … 205 205 return EE_INVALID; 206 206 } 207 207 208 208 unsigned int flags = 0; 209 209 210 210 if (entry->p_flags & PF_X) 211 211 flags |= AS_AREA_EXEC; 212 212 213 213 if (entry->p_flags & PF_W) 214 214 flags |= AS_AREA_WRITE; 215 215 216 216 if (entry->p_flags & PF_R) 217 217 flags |= AS_AREA_READ; 218 218 219 219 flags |= AS_AREA_CACHEABLE; 220 220 221 221 /* 222 222 * Align vaddr down, inserting a little "gap" at the beginning. … … 226 226 uintptr_t base = ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE); 227 227 size_t mem_sz = entry->p_memsz + (entry->p_vaddr - base); 228 228 229 229 as_area_t *area = as_area_create(as, flags, mem_sz, 230 230 AS_AREA_ATTR_NONE, &elf_backend, &backend_data, &base, 0); 231 231 if (!area) 232 232 return EE_MEMORY; 233 233 234 234 /* 235 235 * The segment will be mapped on demand by elf_page_fault(). 236 236 * 237 237 */ 238 238 239 239 return EE_OK; 240 240 } … … 266 266 break; 267 267 } 268 268 269 269 return EE_OK; 270 270 } -
kernel/generic/src/lib/gsort.c
r3061bc1 ra35b458 75 75 { 76 76 size_t i = 0; 77 77 78 78 while (i < cnt) { 79 79 if ((i != 0) && … … 109 109 uint8_t ibuf_slot[IBUF_SIZE]; 110 110 void *slot; 111 111 112 112 if (elem_size > IBUF_SIZE) { 113 113 slot = (void *) malloc(elem_size, 0); … … 116 116 } else 117 117 slot = (void *) ibuf_slot; 118 118 119 119 _gsort(data, cnt, elem_size, cmp, arg, slot); 120 120 121 121 if (elem_size > IBUF_SIZE) 122 122 free(slot); 123 123 124 124 return true; 125 125 } -
kernel/generic/src/lib/halt.c
r3061bc1 ra35b458 55 55 #if (defined(CONFIG_DEBUG)) && (defined(CONFIG_KCONSOLE)) 56 56 bool rundebugger = false; 57 57 58 58 if (!atomic_get(&haltstate)) { 59 59 atomic_set(&haltstate, 1); … … 63 63 atomic_set(&haltstate, 1); 64 64 #endif 65 65 66 66 interrupts_disable(); 67 67 68 68 #if (defined(CONFIG_DEBUG)) && (defined(CONFIG_KCONSOLE)) 69 69 if ((rundebugger) && (kconsole_check_poll())) 70 70 kconsole("panic", "\nLast resort kernel console ready.\n", false); 71 71 #endif 72 72 73 73 if (CPU) 74 74 log(LF_OTHER, LVL_NOTE, "cpu%u: halted", CPU->id); 75 75 else 76 76 log(LF_OTHER, LVL_NOTE, "cpu: halted"); 77 77 78 78 cpu_halt(); 79 79 } -
kernel/generic/src/lib/mem.c
r3061bc1 ra35b458 72 72 size_t i; 73 73 uint16_t *ptr = (uint16_t *) dst; 74 74 75 75 for (i = 0; i < cnt; i++) 76 76 ptr[i] = val; … … 94 94 if (src == dst) 95 95 return dst; 96 96 97 97 /* Non-overlapping? */ 98 98 if ((dst >= src + cnt) || (src >= dst + cnt)) 99 99 return memcpy(dst, src, cnt); 100 100 101 101 uint8_t *dp; 102 102 const uint8_t *sp; 103 103 104 104 /* Which direction? */ 105 105 if (src > dst) { … … 107 107 dp = dst; 108 108 sp = src; 109 109 110 110 while (cnt-- != 0) 111 111 *dp++ = *sp++; … … 114 114 dp = dst + (cnt - 1); 115 115 sp = src + (cnt - 1); 116 116 117 117 while (cnt-- != 0) 118 118 *dp-- = *sp--; 119 119 } 120 120 121 121 return dst; 122 122 } -
kernel/generic/src/lib/memfnc.c
r3061bc1 ra35b458 57 57 { 58 58 uint8_t *dp = (uint8_t *) dst; 59 59 60 60 while (cnt-- != 0) 61 61 *dp++ = val; 62 62 63 63 return dst; 64 64 } … … 80 80 uint8_t *dp = (uint8_t *) dst; 81 81 const uint8_t *sp = (uint8_t *) src; 82 82 83 83 while (cnt-- != 0) 84 84 *dp++ = *sp++; 85 85 86 86 return dst; 87 87 } -
kernel/generic/src/lib/ra.c
r3061bc1 ra35b458 304 304 succ->flags |= RA_SEGMENT_FREE; 305 305 } 306 307 306 307 308 308 /* Put unneeded parts back. */ 309 309 if (pred) { -
kernel/generic/src/lib/rd.c
r3061bc1 ra35b458 58 58 uintptr_t base = (uintptr_t) data; 59 59 assert((base % FRAME_SIZE) == 0); 60 60 61 61 rd_parea.pbase = base; 62 62 rd_parea.frames = SIZE2FRAMES(size); … … 64 64 rd_parea.mapped = false; 65 65 ddi_parea_register(&rd_parea); 66 66 67 67 sysinfo_set_item_val("rd", NULL, true); 68 68 sysinfo_set_item_val("rd.size", NULL, size); 69 69 sysinfo_set_item_val("rd.address.physical", NULL, (sysarg_t) base); 70 70 71 71 log(LF_OTHER, LVL_NOTE, "RAM disk at %p (size %zu bytes)", (void *) base, 72 72 size); -
kernel/generic/src/lib/str.c
r3061bc1 ra35b458 151 151 if (*offset + 1 > size) 152 152 return 0; 153 153 154 154 /* First byte read from string */ 155 155 uint8_t b0 = (uint8_t) str[(*offset)++]; 156 156 157 157 /* Determine code length */ 158 158 159 159 unsigned int b0_bits; /* Data bits in first byte */ 160 160 unsigned int cbytes; /* Number of continuation bytes */ 161 161 162 162 if ((b0 & 0x80) == 0) { 163 163 /* 0xxxxxxx (Plain ASCII) */ … … 180 180 return U_SPECIAL; 181 181 } 182 182 183 183 if (*offset + cbytes > size) 184 184 return U_SPECIAL; 185 185 186 186 wchar_t ch = b0 & LO_MASK_8(b0_bits); 187 187 188 188 /* Decode continuation bytes */ 189 189 while (cbytes > 0) { 190 190 uint8_t b = (uint8_t) str[(*offset)++]; 191 191 192 192 /* Must be 10xxxxxx */ 193 193 if ((b & 0xc0) != 0x80) 194 194 return U_SPECIAL; 195 195 196 196 /* Shift data bits to ch */ 197 197 ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS)); 198 198 cbytes--; 199 199 } 200 200 201 201 return ch; 202 202 } … … 221 221 if (*offset >= size) 222 222 return EOVERFLOW; 223 223 224 224 if (!chr_check(ch)) 225 225 return EINVAL; 226 226 227 227 /* Unsigned version of ch (bit operations should only be done 228 228 on unsigned types). */ 229 229 uint32_t cc = (uint32_t) ch; 230 230 231 231 /* Determine how many continuation bytes are needed */ 232 232 233 233 unsigned int b0_bits; /* Data bits in first byte */ 234 234 unsigned int cbytes; /* Number of continuation bytes */ 235 235 236 236 if ((cc & ~LO_MASK_32(7)) == 0) { 237 237 b0_bits = 7; … … 250 250 return EINVAL; 251 251 } 252 252 253 253 /* Check for available space in buffer */ 254 254 if (*offset + cbytes >= size) 255 255 return EOVERFLOW; 256 256 257 257 /* Encode continuation bytes */ 258 258 unsigned int i; … … 261 261 cc = cc >> CONT_BITS; 262 262 } 263 263 264 264 /* Encode first byte */ 265 265 str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1); 266 266 267 267 /* Advance offset */ 268 268 *offset += cbytes + 1; 269 269 270 270 return EOK; 271 271 } … … 284 284 { 285 285 size_t size = 0; 286 286 287 287 while (*str++ != 0) 288 288 size++; 289 289 290 290 return size; 291 291 } … … 323 323 size_t len = 0; 324 324 size_t offset = 0; 325 325 326 326 while (len < max_len) { 327 327 if (str_decode(str, &offset, STR_NO_LIMIT) == 0) 328 328 break; 329 329 330 330 len++; 331 331 } 332 332 333 333 return offset; 334 334 } … … 363 363 size_t len = 0; 364 364 size_t offset = 0; 365 365 366 366 while (str_decode(str, &offset, STR_NO_LIMIT) != 0) 367 367 len++; 368 368 369 369 return len; 370 370 } … … 380 380 { 381 381 size_t len = 0; 382 382 383 383 while (*wstr++ != 0) 384 384 len++; 385 385 386 386 return len; 387 387 } … … 399 399 size_t len = 0; 400 400 size_t offset = 0; 401 401 402 402 while (str_decode(str, &offset, size) != 0) 403 403 len++; 404 404 405 405 return len; 406 406 } … … 419 419 size_t limit = ALIGN_DOWN(size, sizeof(wchar_t)); 420 420 size_t offset = 0; 421 421 422 422 while ((offset < limit) && (*str++ != 0)) { 423 423 len++; 424 424 offset += sizeof(wchar_t); 425 425 } 426 426 427 427 return len; 428 428 } … … 437 437 if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 127)) 438 438 return true; 439 439 440 440 return false; 441 441 } … … 450 450 if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 1114111)) 451 451 return true; 452 452 453 453 return false; 454 454 } … … 476 476 wchar_t c1 = 0; 477 477 wchar_t c2 = 0; 478 478 479 479 size_t off1 = 0; 480 480 size_t off2 = 0; … … 486 486 if (c1 < c2) 487 487 return -1; 488 488 489 489 if (c1 > c2) 490 490 return 1; … … 523 523 wchar_t c1 = 0; 524 524 wchar_t c2 = 0; 525 525 526 526 size_t off1 = 0; 527 527 size_t off2 = 0; 528 528 529 529 size_t len = 0; 530 530 … … 569 569 assert(size > 0); 570 570 assert(src != NULL); 571 571 572 572 size_t src_off = 0; 573 573 size_t dest_off = 0; 574 574 575 575 wchar_t ch; 576 576 while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) { … … 578 578 break; 579 579 } 580 580 581 581 dest[dest_off] = '\0'; 582 582 } … … 602 602 /* There must be space for a null terminator in the buffer. */ 603 603 assert(size > 0); 604 604 605 605 size_t src_off = 0; 606 606 size_t dest_off = 0; 607 607 608 608 wchar_t ch; 609 609 while ((ch = str_decode(src, &src_off, n)) != 0) { … … 611 611 break; 612 612 } 613 613 614 614 dest[dest_off] = '\0'; 615 615 } … … 636 636 char *dest = malloc(size, 0); 637 637 assert(dest); 638 638 639 639 str_cpy(dest, size, src); 640 640 return dest; … … 666 666 if (size > n) 667 667 size = n; 668 668 669 669 char *dest = malloc(size + 1, 0); 670 670 assert(dest); 671 671 672 672 str_ncpy(dest, size + 1, src, size); 673 673 return dest; … … 695 695 src_idx = 0; 696 696 dest_off = 0; 697 697 698 698 while ((ch = src[src_idx++]) != 0) { 699 699 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK) … … 717 717 size_t off = 0; 718 718 size_t last = 0; 719 719 720 720 while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) { 721 721 if (acc == ch) … … 723 723 last = off; 724 724 } 725 725 726 726 return NULL; 727 727 } … … 744 744 { 745 745 size_t len = wstr_length(str); 746 746 747 747 if ((pos > len) || (pos + 1 > max_pos)) 748 748 return false; 749 749 750 750 size_t i; 751 751 for (i = len; i + 1 > pos; i--) 752 752 str[i + 1] = str[i]; 753 753 754 754 str[pos] = ch; 755 755 756 756 return true; 757 757 } … … 772 772 { 773 773 size_t len = wstr_length(str); 774 774 775 775 if (pos >= len) 776 776 return false; 777 777 778 778 size_t i; 779 779 for (i = pos + 1; i <= len; i++) 780 780 str[i - 1] = str[i]; 781 781 782 782 return true; 783 783 } … … 800 800 assert(neg != NULL); 801 801 assert(result != NULL); 802 802 803 803 *neg = false; 804 804 const char *str = nptr; 805 805 806 806 /* Ignore leading whitespace */ 807 807 while (isspace(*str)) 808 808 str++; 809 809 810 810 if (*str == '-') { 811 811 *neg = true; … … 813 813 } else if (*str == '+') 814 814 str++; 815 815 816 816 if (base == 0) { 817 817 /* Decode base if not specified */ 818 818 base = 10; 819 819 820 820 if (*str == '0') { 821 821 base = 8; 822 822 str++; 823 823 824 824 switch (*str) { 825 825 case 'b': … … 856 856 } 857 857 } 858 858 859 859 *result = 0; 860 860 const char *startstr = str; 861 861 862 862 while (*str != 0) { 863 863 unsigned int digit; 864 864 865 865 if ((*str >= 'a') && (*str <= 'z')) 866 866 digit = *str - 'a' + 10; … … 871 871 else 872 872 break; 873 873 874 874 if (digit >= base) 875 875 break; 876 876 877 877 uint64_t prev = *result; 878 878 *result = (*result) * base + digit; 879 879 880 880 if (*result < prev) { 881 881 /* Overflow */ … … 883 883 return EOVERFLOW; 884 884 } 885 885 886 886 str++; 887 887 } 888 888 889 889 if (str == startstr) { 890 890 /* … … 894 894 str = nptr; 895 895 } 896 896 897 897 *endptr = (char *) str; 898 898 899 899 if (str == nptr) 900 900 return EINVAL; 901 901 902 902 return EOK; 903 903 } … … 919 919 { 920 920 assert(result != NULL); 921 921 922 922 bool neg; 923 923 char *lendptr; 924 924 errno_t ret = str_uint(nptr, &lendptr, base, &neg, result); 925 925 926 926 if (endptr != NULL) 927 927 *endptr = (char *) lendptr; 928 928 929 929 if (ret != EOK) 930 930 return ret; 931 931 932 932 /* Do not allow negative values */ 933 933 if (neg) 934 934 return EINVAL; 935 935 936 936 /* Check whether we are at the end of 937 937 the string in strict mode */ 938 938 if ((strict) && (*lendptr != 0)) 939 939 return EINVAL; 940 940 941 941 return EOK; 942 942 }
Note:
See TracChangeset
for help on using the changeset viewer.