|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Amazon.com, Inc. or its affiliates. |
| 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 are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer in the documentation |
| 12 | + * and/or other materials provided with the distribution. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 15 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 18 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 19 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 20 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 21 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 23 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + */ |
| 25 | +#include <console.h> |
| 26 | +#include <cpuid.h> |
| 27 | +#include <ktf.h> |
| 28 | +#include <real_mode.h> |
| 29 | +#include <sched.h> |
| 30 | +#include <string.h> |
| 31 | +#include <symbols.h> |
| 32 | +#include <test.h> |
| 33 | + |
| 34 | +extern char *kernel_cmdline; |
| 35 | +#include <cmdline.h> |
| 36 | + |
| 37 | +static char opt_string[4]; |
| 38 | +string_cmd("string", opt_string); |
| 39 | + |
| 40 | +static char opt_badstring[5]; |
| 41 | +string_cmd("badstring", opt_badstring); |
| 42 | + |
| 43 | +static unsigned long opt_ulong; |
| 44 | +ulong_cmd("integer", opt_ulong); |
| 45 | + |
| 46 | +static bool opt_bool = 0; |
| 47 | +bool_cmd("boolean", opt_bool); |
| 48 | + |
| 49 | +static bool opt_booltwo = 0; |
| 50 | +bool_cmd("booleantwo", opt_booltwo); |
| 51 | + |
| 52 | +static char memmove_string[4]; |
| 53 | +static char range_string[] = "123456"; |
| 54 | +static char *src, *dst; |
| 55 | + |
| 56 | +static void cpu_freq_expect(const char *cpu_str, uint64_t expectation) { |
| 57 | + uint64_t result = get_cpu_freq(cpu_str); |
| 58 | + if (result != expectation) { |
| 59 | + printk("Could not parse cpu frequency from string '%s'\n", cpu_str); |
| 60 | + printk("Expectation vs. result: %llu vs. %llu\n", expectation, result); |
| 61 | + BUG(); |
| 62 | + } |
| 63 | + |
| 64 | + printk("Got CPU string '%s' and frequency '%llu'\n", cpu_str, result); |
| 65 | + return; |
| 66 | +} |
| 67 | + |
| 68 | +static int __user_text func(void *arg) { return 0; } |
| 69 | + |
| 70 | +int unit_tests(void *_unused) { |
| 71 | + usermode_call(func, NULL); |
| 72 | + |
| 73 | + printk("\nLet the UNITTESTs begin\n"); |
| 74 | + printk("Commandline parsing: %s\n", kernel_cmdline); |
| 75 | + |
| 76 | + if (strcmp(opt_string, "foo")) { |
| 77 | + printk("String parameter opt_string != foo: %s\n", opt_string); |
| 78 | + BUG(); |
| 79 | + } |
| 80 | + else { |
| 81 | + printk("String parameter parsing works!\n"); |
| 82 | + } |
| 83 | + |
| 84 | + if (strcmp(opt_badstring, "tool")) { |
| 85 | + printk("String parameter opt_badstring != tool: %s\n", opt_badstring); |
| 86 | + BUG(); |
| 87 | + } |
| 88 | + else { |
| 89 | + printk("String parameter parsing works!\n"); |
| 90 | + } |
| 91 | + |
| 92 | + if (opt_ulong != 42) { |
| 93 | + printk("Integer parameter opt_ulong != 42: %d\n", opt_ulong); |
| 94 | + BUG(); |
| 95 | + } |
| 96 | + else { |
| 97 | + printk("Integer parameter parsing works!\n"); |
| 98 | + } |
| 99 | + |
| 100 | + if (!opt_bool || !opt_booltwo) { |
| 101 | + printk("Boolean parameter opt_bool != true: %d\n", opt_bool); |
| 102 | + printk("Boolean parameter opt_booltwo != true: %d\n", opt_booltwo); |
| 103 | + BUG(); |
| 104 | + } |
| 105 | + else { |
| 106 | + printk("Boolean parameter parsing works!\n"); |
| 107 | + } |
| 108 | + |
| 109 | + printk("\nMemmove testing:\n"); |
| 110 | + (void) memmove(memmove_string, opt_string, sizeof(opt_string)); |
| 111 | + if (!strcmp(memmove_string, opt_string)) { |
| 112 | + printk("Moving around memory works!\n"); |
| 113 | + } |
| 114 | + else { |
| 115 | + printk("Memmove'ing did not work: %s (%p) != %s (%p)\n", memmove_string, |
| 116 | + memmove_string, opt_string, opt_string); |
| 117 | + } |
| 118 | + |
| 119 | + src = (char *) range_string; |
| 120 | + dst = (char *) range_string + 2; |
| 121 | + (void) memmove(dst, src, 4); |
| 122 | + if (!strcmp(range_string, "121234")) { |
| 123 | + printk("Moving around memory with overlaping ranges works!\n"); |
| 124 | + } |
| 125 | + else { |
| 126 | + printk("Overlaping memmove'ing did not work: %s != %s\n", range_string, "121234"); |
| 127 | + } |
| 128 | + |
| 129 | + cpu_freq_expect("Intel(R) Xeon(R) CPU E3-1270 V2 @ 3.50GHz", 3500000000); |
| 130 | + cpu_freq_expect("Intel(R) Celeron(R) CPU J1900 @ 1.99GHz", 1990000000); |
| 131 | + cpu_freq_expect("AMD G-T48L Processor", 0); |
| 132 | + cpu_freq_expect("Intel(R) Atom(TM) CPU E3815 @ 1.46GHz", 1460000000); |
| 133 | + cpu_freq_expect("Intel(R) Atom(TM) CPU E620 @ 600MHz", 600000000); |
| 134 | + cpu_freq_expect("VIA C7 Processor 1000MHz", 1000000000); |
| 135 | + cpu_freq_expect("Intel(R) Core(TM) i7 CPU 950 @ 3.07GHz", 3070000000); |
| 136 | + cpu_freq_expect("AMD Ryzen Threadripper 1950X 16-Core Processor", 0); |
| 137 | + cpu_freq_expect("Prototyp Amazing Foo One @ 1GHz", 1000000000); |
| 138 | + cpu_freq_expect("Prototyp Amazing Foo Two @ 1.00GHz", 1000000000); |
| 139 | + |
| 140 | + printk("Long mode to real mode transition:\n"); |
| 141 | + long_to_real(); |
| 142 | + |
| 143 | + return 0; |
| 144 | +} |
0 commit comments