Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | Copyright (c) 2005 Eliot Dresselhaus |
| 17 | |
| 18 | Permission is hereby granted, free of charge, to any person obtaining |
| 19 | a copy of this software and associated documentation files (the |
| 20 | "Software"), to deal in the Software without restriction, including |
| 21 | without limitation the rights to use, copy, modify, merge, publish, |
| 22 | distribute, sublicense, and/or sell copies of the Software, and to |
| 23 | permit persons to whom the Software is furnished to do so, subject to |
| 24 | the following conditions: |
| 25 | |
| 26 | The above copyright notice and this permission notice shall be |
| 27 | included in all copies or substantial portions of the Software. |
| 28 | |
| 29 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 30 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 32 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 33 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 34 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 35 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 | */ |
| 37 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 38 | #include <vppinfra/clib.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | #include <vppinfra/longjmp.h> |
| 40 | #include <vppinfra/format.h> |
| 41 | |
| 42 | static void test_calljmp (unformat_input_t * input); |
| 43 | |
| 44 | static int i; |
| 45 | |
| 46 | static int verbose; |
| 47 | #define if_verbose(format,args...) \ |
| 48 | if (verbose) { clib_warning(format, ## args); } |
| 49 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 50 | static never_inline void |
| 51 | f2 (clib_longjmp_t * env) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | { |
| 53 | i++; |
| 54 | clib_longjmp (env, 1); |
| 55 | } |
| 56 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 57 | static never_inline void |
| 58 | f1 (clib_longjmp_t * env) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | { |
| 60 | i++; |
| 61 | f2 (env); |
| 62 | } |
| 63 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 64 | int |
| 65 | test_longjmp_main (unformat_input_t * input) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | { |
| 67 | clib_longjmp_t env; |
| 68 | |
| 69 | i = 0; |
| 70 | if (clib_setjmp (&env, 0) == 0) |
| 71 | { |
| 72 | if_verbose ("calling long jumper %d", i); |
| 73 | f1 (&env); |
| 74 | } |
| 75 | if_verbose ("back from long jump %d", i); |
| 76 | |
| 77 | test_calljmp (input); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 82 | static uword |
| 83 | f3 (uword arg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | { |
| 85 | uword i, j, array[10]; |
| 86 | |
| 87 | for (i = 0; i < ARRAY_LEN (array); i++) |
| 88 | array[i] = arg + i; |
| 89 | |
| 90 | j = 0; |
| 91 | for (i = 0; i < ARRAY_LEN (array); i++) |
| 92 | j ^= array[i]; |
| 93 | |
| 94 | return j; |
| 95 | } |
| 96 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 97 | static void |
| 98 | test_calljmp (unformat_input_t * input) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 100 | static u8 stack[32 * 1024] __attribute__ ((aligned (16))); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | uword v; |
| 102 | |
| 103 | v = clib_calljmp (f3, 0, stack + sizeof (stack)); |
| 104 | ASSERT (v == f3 (0)); |
| 105 | if_verbose ("calljump ok"); |
| 106 | } |
| 107 | |
| 108 | #ifdef CLIB_UNIX |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 109 | int |
| 110 | main (int argc, char *argv[]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | { |
| 112 | unformat_input_t i; |
| 113 | int res; |
| 114 | |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 115 | clib_mem_init (0, 64 << 20); |
| 116 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | verbose = (argc > 1); |
| 118 | unformat_init_command_line (&i, argv); |
| 119 | res = test_longjmp_main (&i); |
| 120 | unformat_free (&i); |
| 121 | return res; |
| 122 | } |
| 123 | #endif |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * fd.io coding-style-patch-verification: ON |
| 127 | * |
| 128 | * Local Variables: |
| 129 | * eval: (c-set-style "gnu") |
| 130 | * End: |
| 131 | */ |