-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexception.cpp
More file actions
2312 lines (2080 loc) · 75 KB
/
exception.cpp
File metadata and controls
2312 lines (2080 loc) · 75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2024 - 2025 Khalil Estell and the libhal contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <algorithm>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <exception>
#include <memory_resource>
#include <span>
#include <typeinfo>
#include <libhal-exceptions/control.hpp>
#include "internal.hpp"
void start_sub();
void end_sub();
// NOLINTBEGIN(bugprone-reserved-identifier)
// NOLINTBEGIN(readability-identifier-naming)
extern "C"
{
void _exit([[maybe_unused]] int rc);
void* __wrap___cxa_allocate_exception(size_t);
void __wrap___cxa_free_exception(void*);
void __wrap___cxa_call_unexpected(void*);
void __wrap___cxa_end_catch();
void* __wrap___cxa_begin_catch(void*);
void __wrap___cxa_end_cleanup();
void __wrap__Unwind_Resume(void*);
void __wrap___cxa_rethrow() noexcept(false);
void __wrap___cxa_throw(ke::exception_ptr p_thrown_exception,
std::type_info* p_type_info,
ke::destructor_t p_destructor) noexcept(false);
} // extern "C"
// NOLINTEND(readability-identifier-naming)
// NOLINTEND(bugprone-reserved-identifier)
namespace ke {
struct instructions_t
{
// Why the length of 8? ARM unwind instructions are capped at 7 bytes for all
// possible functions. The 8th instruction will always be the finish byte.
// This allows the unwinder to iterate through the whole list without needing
// to compare a length variable which would decrease performance.
std::array<std::uint8_t, 8> data{
arm_ehabi::finish, arm_ehabi::finish, arm_ehabi::finish, arm_ehabi::finish,
arm_ehabi::finish, arm_ehabi::finish, arm_ehabi::finish, arm_ehabi::finish,
};
};
exception_control_block control_block{};
su16_t* get_su16(void* p_ptr)
{
return reinterpret_cast<su16_t*>(p_ptr);
}
lu_t* get_lu(void* p_ptr)
{
return reinterpret_cast<lu_t*>(p_ptr);
}
/**
* @brief Captures all registers CPU registers
*
* Registers captured are R4-R12 and SP. The LR (link) register is copied into
* the PC register of the `ke::cortex_m_cpu&`. The LR member variable of the
* input parameter is left as is and will be updated as the unwinding proceeds.
*
*/
[[gnu::naked, gnu::noinline]] void capture_cpu_core(ke::cortex_m_cpu&)
{
asm volatile(
// ARM calling conventions states that arguments are passed into R0-R3.
// The single reference parameter p_cpu_core is in R0.
//
// Add 16 bytes to the address within R0 to skip over R0-R3. We assume
// `__cxa_throw` & `__cxa_rethrow` will never utilize the `sp = r[nnnn]`
// instruction, meaning that the non-callee preserved registers can be
// skipped. R0 now points to `ke::cortex_m_cpu::r4`
"add r0, #16\n"
// Store R4 to R12 into `ke::cortex_m_cpu::r4` to `ke::cortex_m_cpu::r12`.
// The ! means, increment R<N> by the number of words stored. R0 now points
// at the `ke::cortex_m_cpu&`'s SP register.
"stmia r0!, {r4-r12}\n"
// Store SP into [R0] (`ke::cortex_m_cpu&`'s SP register)
"str sp, [r0]\n"
// Store LR into [R0 + 8] (`ke::cortex_m_cpu&`'s PC register)
"str lr, [r0, #8]\n"
"bx lr\n"
"\n");
}
struct index_less_than
{
bool operator()(index_entry_t const& left, index_entry_t const& right)
{
return left.function() < right.function();
}
bool operator()(index_entry_t const& left, std::uint32_t right)
{
// De Morgan's laws to flip the < sign. Otherwise I have to cast.
return right >= left.function();
}
bool operator()(std::uint32_t left, index_entry_t const& right)
{
return left < right.function();
}
bool operator()(std::uint32_t left, std::uint32_t right)
{
return left < right;
}
};
std::span<index_entry_t const> get_arm_exception_index()
{
return { &__exidx_start, &__exidx_end };
}
// NOLINTBEGIN(bugprone-reserved-identifier)
// NOLINTBEGIN(readability-identifier-naming)
namespace __except_abi::inline v1 {
// This is here for documentation purposes. This will change and should not be
// considered to be stable.
struct nearpoint_descriptor
{
std::uint32_t normal_block_size = 0;
std::uint32_t text_starting_address = 0;
std::uint32_t small_block_size = 0;
std::uint32_t small_starting_address = 0;
};
[[gnu::weak]] std::span<std::uint32_t const> near_point_descriptor{};
[[gnu::weak]] std::span<std::uint32_t const> normal_table{};
[[gnu::weak]] std::span<std::uint32_t const> small_table{};
} // namespace __except_abi::inline v1
// NOLINTEND(readability-identifier-naming)
// NOLINTEND(bugprone-reserved-identifier)
index_entry_t const& get_index_entry_near_point(std::uint32_t p_program_counter)
{
auto const index_table = get_arm_exception_index();
auto const block_power = ke::__except_abi::near_point_descriptor[0];
auto const program_offset = ke::__except_abi::near_point_descriptor[1];
auto const pc = p_program_counter - program_offset;
auto const block_index = pc >> block_power;
auto const linear_info = ke::__except_abi::normal_table[block_index];
auto const entry_start = linear_info >> block_power;
auto const inter_block_mask = (1U << block_power) - 1U;
auto const entry_count = linear_info & inter_block_mask;
auto const inter_block_location = pc & inter_block_mask;
auto const scaled = inter_block_location * entry_count;
auto const guess_offset = scaled >> block_power;
auto const initial_guess = static_cast<ptrdiff_t>(entry_start + guess_offset);
auto guess = index_table.begin() + initial_guess;
if (p_program_counter < guess->function()) {
do {
--guess;
} while (guess->function() > p_program_counter);
} else {
do {
++guess;
} while (guess->function() <= p_program_counter);
--guess;
}
return *guess;
}
index_entry_t const& get_index_entry(std::uint32_t p_program_counter)
{
auto const index_table = get_arm_exception_index();
auto const& index =
std::ranges::upper_bound(index_table, p_program_counter, index_less_than{});
if (index == index_table.begin()) {
return *index;
}
return *(index - 1);
}
std::uintptr_t near_point_guess_index(std::uintptr_t p_program_counter)
{
auto const block_power = ke::__except_abi::near_point_descriptor[0];
auto const program_offset = ke::__except_abi::near_point_descriptor[1];
auto const pc = p_program_counter - program_offset;
auto const inter_block_mask = (1U << block_power) - 1U;
auto const inter_block_location = pc & inter_block_mask;
auto const block_index = pc >> block_power;
auto const linear_info = ke::__except_abi::normal_table[block_index];
auto const average_size = linear_info & inter_block_mask;
auto const entry_start = linear_info >> block_power;
if (average_size == 0) {
return entry_start;
}
auto const guess_offset = inter_block_location / average_size;
auto const location = entry_start + guess_offset;
return location;
}
[[gnu::always_inline]] inline constexpr std::uint32_t read_uleb128(
std::uint8_t const** p_ptr)
{
std::uint32_t result = 0;
std::uint8_t shift_amount = 0;
while (true) {
std::uint8_t const uleb128 = **p_ptr;
result |= (uleb128 & 0x7F) << shift_amount;
(*p_ptr)++;
if ((uleb128 == 0x80) == 0x00) {
break;
}
shift_amount += 7;
}
return result;
}
[[gnu::always_inline]] inline constexpr std::int32_t read_sleb128(
std::uint8_t const** p_ptr)
{
constexpr std::uint8_t leb128_bits = 7;
std::int32_t result = 0;
std::uint32_t shift_amount = 0;
// No number we deal with on the 32-bit system will or should exceed 31-bits
// of information
for (std::size_t i = 0; i < sizeof(std::int32_t); i++) {
std::uint8_t const sleb128 = **p_ptr;
result |= (sleb128 & 0x7F) << shift_amount;
(*p_ptr)++;
if ((sleb128 & 0x80) == 0x00) {
auto const bytes_consumed = i + 1;
auto const loaded_bits = bytes_consumed * leb128_bits;
auto const ext_shift_amount =
static_cast<std::int32_t>(32L - loaded_bits);
// Shift to the left up to the signed MSB bit
result <<= ext_shift_amount;
// Arithmetic shift right to sign extend number
result >>= ext_shift_amount;
break;
}
shift_amount += 7;
}
return result;
}
template<size_t DecodedCount>
struct decoded_uleb128_t
{
std::array<std::uint32_t, DecodedCount> data{};
std::uint8_t const* last_read;
};
template<size_t N>
inline constexpr decoded_uleb128_t<N> multi_read_uleb128(
std::uint8_t const* p_ptr)
{
decoded_uleb128_t<N> result{};
auto iter = result.data.begin();
std::uint32_t shift_amount = 0;
// THIS IS WRONG! ULEB is LSB first then ends on MSB. This is big endian
while (iter != result.data.end()) {
std::uint8_t const uleb128 = *(p_ptr++);
*iter |= (uleb128 & 0x7F) << shift_amount;
if ((uleb128 & 0x80) == 0x00) {
iter++;
shift_amount = 0;
continue;
}
shift_amount += 7;
}
result.last_read = p_ptr;
return result;
}
template<typename T>
T const* as(void const* p_ptr)
{
return reinterpret_cast<T const*>(p_ptr);
}
/**
* @brief Dwarf exception handling personality encodings
*
* Spec:
* https://refspecs.linuxfoundation.org/LSB_1.3.0/gLSB/gLSB/ehframehdr.html
*/
enum class lsda_encoding : std::uint8_t
{
absptr = 0x00,
uleb128 = 0x01,
udata2 = 0x02,
udata4 = 0x03,
udata8 = 0x04,
sleb128 = 0x09,
sdata2 = 0x0A,
sdata4 = 0x0B,
sdata8 = 0x0C,
pcrel = 0x10,
textrel = 0x20,
datarel = 0x30,
funcrel = 0x40,
aligned = 0x50,
// no data follows
omit = 0xff,
};
constexpr lsda_encoding operator&(lsda_encoding const& p_encoding,
std::uint8_t const& p_byte)
{
return static_cast<lsda_encoding>(static_cast<std::uint8_t>(p_encoding) &
p_byte);
}
constexpr lsda_encoding operator&(lsda_encoding const& p_encoding,
lsda_encoding const& p_byte)
{
return static_cast<lsda_encoding>(static_cast<std::uint8_t>(p_encoding) &
static_cast<std::uint8_t>(p_byte));
}
template<lsda_encoding encoding>
[[gnu::always_inline]] inline std::uintptr_t read_encoded_data(
std::uint8_t const** p_data)
{
std::uint8_t const* ptr = *p_data;
std::uintptr_t result = 0;
if constexpr (encoding == lsda_encoding::omit) {
return 0;
}
static constexpr auto encoding_type = encoding & 0x0F;
if constexpr (encoding_type == lsda_encoding::absptr) {
result = *as<uintptr_t>(ptr);
ptr += sizeof(uintptr_t);
}
if constexpr (encoding_type == lsda_encoding::uleb128) {
result = read_uleb128(&ptr);
}
if constexpr (encoding_type == lsda_encoding::udata2) {
result = *as<uint16_t>(ptr);
ptr += sizeof(uint16_t);
}
if constexpr (encoding_type == lsda_encoding::udata4) {
result = *as<uint32_t>(ptr);
ptr += sizeof(uint32_t);
}
if constexpr (encoding_type == lsda_encoding::sdata2) {
result = *as<int16_t>(ptr);
ptr += sizeof(int16_t);
}
if constexpr (encoding_type == lsda_encoding::sdata4) {
result = *as<int32_t>(ptr);
ptr += sizeof(int32_t);
}
if constexpr (encoding_type == lsda_encoding::sdata8) {
result = *as<int64_t>(ptr);
ptr += sizeof(int64_t);
}
if constexpr (encoding_type == lsda_encoding::udata8) {
result = *as<uint64_t>(ptr);
ptr += sizeof(uint64_t);
}
if constexpr (encoding_type == lsda_encoding::sleb128) {
result = read_sleb128(&ptr);
}
// Handle indirection GCC extension
if constexpr (static_cast<bool>(encoding & 0x80)) {
// NOLINTNEXTLINE(performance-no-int-to-ptr)
result = *reinterpret_cast<std::uintptr_t const*>(result);
}
*p_data = ptr;
return result;
}
[[gnu::always_inline]] inline std::uintptr_t read_encoded_data(
std::uint8_t const** p_data,
lsda_encoding p_encoding)
{
std::uintptr_t result = 0;
if (p_encoding == lsda_encoding::omit) {
return 0;
}
auto const encoding_type = p_encoding & 0x0F;
switch (encoding_type) {
case lsda_encoding::absptr:
result = read_encoded_data<lsda_encoding::absptr>(p_data);
break;
case lsda_encoding::uleb128:
result = read_encoded_data<lsda_encoding::uleb128>(p_data);
break;
case lsda_encoding::udata2:
result = read_encoded_data<lsda_encoding::udata2>(p_data);
break;
case lsda_encoding::udata4:
result = read_encoded_data<lsda_encoding::udata4>(p_data);
break;
case lsda_encoding::sdata2:
result = read_encoded_data<lsda_encoding::sdata2>(p_data);
break;
case lsda_encoding::sdata4:
result = read_encoded_data<lsda_encoding::sdata4>(p_data);
break;
case lsda_encoding::sdata8:
result = read_encoded_data<lsda_encoding::sdata8>(p_data);
break;
case lsda_encoding::udata8:
result = read_encoded_data<lsda_encoding::udata8>(p_data);
break;
case lsda_encoding::sleb128:
result = read_encoded_data<lsda_encoding::sleb128>(p_data);
break;
default:
std::terminate();
break;
}
// Handle indirection GCC extension
if (static_cast<bool>(p_encoding & 0x80)) {
// NOLINTNEXTLINE(performance-no-int-to-ptr)
result = *reinterpret_cast<std::uintptr_t const*>(result);
}
return result;
}
[[gnu::naked]] [[noreturn]] inline void restore_cpu_core(cortex_m_cpu&)
{
asm volatile(
"\n"
// ARM calling conventions states that arguments are passed into R0-R3.
// The single reference parameter p_cpu_core is in R0.
// We move the address to R2, since we need to set r0 and r1 to the
// exception error object's address and filter number respectively
"mov r2, r0\n"
// Since our cortex_m_cpu object has the layout of 16x u32 values, we can
// just iterate through the struct like an array.
//
// This instruction loads R0 and R1 with the contents addressed by R2
"ldmia.w r2, {r0, r1}\n"
// Move address by 16-bytes or 4x 32-bit words which skips past R0 to R3.
// R2 now points to R4 within the `cortex_m_cpu` object.
"add r2, #16\n"
// Load R4 to R12 from `cortex_m_cpu` and increment the register's
// address. R2 now points to the SP register
"ldmia.w r2!, {r4, r5, r6, r7, r8, r9, r10, r11, r12}\n"
// Unfortunately SP cannot be in the list above, so we set it here
"ldr sp, [r2]\n"
// Load LR
"ldr lr, [r2, #4]\n"
// Load PC which will jump us to the landing pad
"ldr pc, [r2, #8]\n"
"");
}
inline void skip_dwarf_info(std::uint8_t const** p_lsda)
{
auto const* lsda = *p_lsda;
auto const format = lsda_encoding{ *(lsda++) };
if (format != lsda_encoding::omit) {
// Ignore this because we don't need it for unwinding.
read_encoded_data(&lsda, format);
}
*p_lsda = lsda;
}
struct lsda_header_info
{
std::uint8_t const* call_site_end = nullptr;
/// If this pointer is behind the call_site end, then there is no type table
/// available.
std::uint8_t const* type_table_end = nullptr;
lsda_encoding type_table_encoding;
lsda_encoding call_site_encoding;
};
inline lsda_header_info parse_header(std::uint8_t const** p_lsda)
{
lsda_header_info info{};
skip_dwarf_info(p_lsda);
// Capture type table end. Will be before call_site_end if it did not exist
auto const* lsda = *p_lsda;
info.type_table_encoding = lsda_encoding{ *(lsda++) };
if (info.type_table_encoding != lsda_encoding::omit) {
auto const offset = read_uleb128(&lsda);
info.type_table_end = lsda + offset;
} else {
info.type_table_end = lsda;
}
info.call_site_encoding = lsda_encoding{ *(lsda++) };
auto const offset = read_uleb128(&lsda);
info.call_site_end = lsda + offset;
*p_lsda = lsda;
return info;
}
inline auto const* to_lsda(exception_control_block& p_exception_object)
{
return reinterpret_cast<std::uint8_t const*>(
index_entry_t::lsda_data(p_exception_object.cache.personality));
}
inline auto calculate_relative_pc(exception_control_block& p_exception_object)
{
return p_exception_object.cache.relative_address() & ~1;
}
struct call_site_info
{
std::uint32_t landing_pad = 0;
std::uint32_t action = 0;
bool unwind = false;
};
template<lsda_encoding encoding>
inline call_site_info parse_call_site(std::uint8_t const** p_lsda,
std::uint32_t p_rel_pc,
std::uint8_t const* p_call_site_end)
{
call_site_info info;
do {
auto start = read_encoded_data<encoding>(p_lsda);
auto length = read_encoded_data<encoding>(p_lsda);
info.landing_pad = read_encoded_data<encoding>(p_lsda);
info.action = read_encoded_data<encoding>(p_lsda);
if (start <= p_rel_pc && p_rel_pc <= start + length) {
if (info.landing_pad == 0) {
info.unwind = true;
}
break;
}
} while (*p_lsda < p_call_site_end);
return info;
}
inline call_site_info parse_uleb128_call_site(
std::uint8_t const* p_lsda,
std::uint32_t p_rel_pc,
std::uint8_t const* p_call_site_end)
{
call_site_info info;
do {
auto const values = multi_read_uleb128<4>(p_lsda);
auto const& start = values.data[0];
auto const& length = values.data[1];
auto const& landing_pad = values.data[2];
auto const& action = values.data[3];
if (start <= p_rel_pc && p_rel_pc <= start + length) {
if (landing_pad == 0) {
info.unwind = true;
} else {
info.landing_pad = landing_pad;
info.action = action;
}
break;
}
p_lsda = values.last_read;
} while (p_lsda < p_call_site_end);
return info;
}
class action_decoder
{
public:
// NOLINTBEGIN(bugprone-easily-swappable-parameters)
action_decoder(std::uint8_t const* p_type_table_end,
std::uint8_t const* p_end_of_callsite,
std::uint32_t p_action)
// NOLINTEND(bugprone-easily-swappable-parameters)
: m_type_table_end(p_type_table_end)
, m_action_position(p_end_of_callsite + (p_action - 1))
{
}
static std::type_info const* to_type_info(void const* p_type_info_address)
{
// NOLINTNEXTLINE(performance-no-int-to-ptr)
return reinterpret_cast<std::type_info const*>(
to_absolute_address(p_type_info_address));
}
static std::type_info const* catch_all()
{
return reinterpret_cast<std::type_info const*>(0xFFFF'FFFF);
}
std::type_info const* get_current_type_info_from_filter()
{
auto const* type_table = as<std::uintptr_t const*>(m_type_table_end);
// We assume prel here because all other options are deprecated
// TODO(#39): Consider using the type table encoding format for decoding
// the type table info rather than assuming that the values here are
// prel31_offsets
auto const* current_type = &type_table[-m_filter];
if (*current_type == nullptr) {
return catch_all();
}
auto const* test =
to_absolute_address_ptr(static_cast<void const*>(current_type));
return reinterpret_cast<std::type_info const*>(test);
}
[[nodiscard]] std::type_info const* get_next_catch_type()
{
if (m_action_position == nullptr) {
return nullptr;
}
do {
m_filter = read_sleb128(&m_action_position);
auto const previous_next_offset_position = m_action_position;
auto const next_action_offset = read_sleb128(&m_action_position);
if (next_action_offset == 0) {
m_action_position = nullptr;
} else {
m_action_position = previous_next_offset_position + next_action_offset;
}
// Negative numbers are for the deprecated `throws()` specifier that we do
// not support. We throw those away and continue looking through the
// action table.
} while (m_filter < 0);
if (m_filter == 0) {
return catch_all();
}
return get_current_type_info_from_filter();
}
[[nodiscard]] constexpr auto filter() const
{
return m_filter;
}
private:
std::uint8_t const* m_type_table_end = nullptr;
std::uint8_t const* m_action_position = nullptr;
std::int32_t m_filter = 0;
};
inline void enter_function(exception_control_block& p_exception_object)
{
auto const* lsda = to_lsda(p_exception_object);
auto info = parse_header(&lsda);
auto const rel_pc = calculate_relative_pc(p_exception_object);
call_site_info site_info{};
switch (info.call_site_encoding) {
case lsda_encoding::uleb128: {
site_info = parse_uleb128_call_site(lsda, rel_pc, info.call_site_end);
break;
}
case lsda_encoding::udata2: {
site_info = parse_call_site<lsda_encoding::udata2>(
&lsda, rel_pc, info.call_site_end);
break;
}
case lsda_encoding::udata4: {
site_info = parse_call_site<lsda_encoding::udata4>(
&lsda, rel_pc, info.call_site_end);
break;
}
case lsda_encoding::udata8: {
site_info = parse_call_site<lsda_encoding::udata8>(
&lsda, rel_pc, info.call_site_end);
break;
}
case lsda_encoding::sdata2: {
site_info = parse_call_site<lsda_encoding::sdata2>(
&lsda, rel_pc, info.call_site_end);
break;
}
case lsda_encoding::sdata4: {
site_info = parse_call_site<lsda_encoding::sdata4>(
&lsda, rel_pc, info.call_site_end);
break;
}
case lsda_encoding::sdata8: {
site_info = parse_call_site<lsda_encoding::sdata8>(
&lsda, rel_pc, info.call_site_end);
break;
}
case lsda_encoding::sleb128: {
site_info = parse_call_site<lsda_encoding::sleb128>(
&lsda, rel_pc, info.call_site_end);
break;
}
default: {
std::terminate();
}
}
if (site_info.unwind) {
p_exception_object.cache.state(runtime_state::unwind_frame);
return;
}
auto& cpu = p_exception_object.cpu;
auto* entry_ptr = p_exception_object.cache.entry_ptr;
// This occurs when a frame has destructors that need cleaning up but no
// try/catch blocks resulting in there being no action table or type table. In
// such a case, then the scope found should be entered immediately!
if (info.type_table_end < info.call_site_end) {
// LSB must be set to 1 to jump to an address
auto const final_destination =
(entry_ptr->function() + site_info.landing_pad) | 0b1;
// Set PC to the cleanup destination
cpu.pc = final_destination;
// Install CPU state
restore_cpu_core(cpu);
}
action_decoder a_decoder(
info.type_table_end, info.call_site_end, site_info.action);
for (auto const* catch_type = a_decoder.get_next_catch_type();
catch_type != nullptr;
catch_type = a_decoder.get_next_catch_type()) {
for (auto const& type_entry : p_exception_object.type_info) {
if (type_entry.type_info == catch_type) {
p_exception_object.choosen_type_offset = type_entry.offset;
// ====== Prepare to Install context!! =====
cpu[0] = &p_exception_object;
cpu[1] = a_decoder.filter();
// LSB must be set to 1 to jump to an address
auto const final_destination =
(entry_ptr->function() + site_info.landing_pad) | 0b1;
// Set PC to the cleanup destination
cpu.pc = final_destination;
// Install CPU state
restore_cpu_core(cpu);
}
}
}
}
template<size_t Amount>
constexpr std::uint32_t vsp_deallocate_amount()
{
return Amount + 1;
}
enum class pop_lr : std::uint8_t
{
skip = 0,
do_it = 1,
};
template<size_t PopCount, pop_lr PopLinkRegister = pop_lr::skip>
[[nodiscard("You MUST set the unwind function's stack pointer to this "
"value after executing it!")]]
inline std::uint32_t const* pop_register_range(std::uint32_t const* sp_ptr,
cortex_m_cpu& p_cpu)
{
// We pull these pointers out in order to access them incrementally, which
// will give the hint to the compiler to convert them into a sequence of
// immediate load and stores.
auto* r4_pointer = &p_cpu.r4.data;
static_assert(PopCount <= 7, "Pop Count cannot be above 7");
// NOTE: A for loop has the same cycle count, and is more compact
if constexpr (PopCount == 0) {
*r4_pointer = *sp_ptr;
} else {
for (std::size_t i = 0; i < PopCount + 1; i++) {
r4_pointer[i] = sp_ptr[i];
}
}
if constexpr (PopLinkRegister == pop_lr::do_it) {
p_cpu.lr = sp_ptr[PopCount + 1];
}
return sp_ptr + PopCount + 1 + unsigned{ PopLinkRegister == pop_lr::do_it };
}
void unwind_frame(instructions_t const& p_instructions, cortex_m_cpu& p_cpu)
{
static constexpr std::array<void*, 256> jump_table{
&&vsp_add_0, // [0]
&&vsp_add_1, // [1]
&&vsp_add_2, // [2]
&&vsp_add_3, // [3]
&&vsp_add_4, // [4]
&&vsp_add_5, // [5]
&&vsp_add_6, // [6]
&&vsp_add_7, // [7]
&&vsp_add_8, // [8]
&&vsp_add_9, // [9]
&&vsp_add_10, // [10]
&&vsp_add_11, // [11]
&&vsp_add_12, // [12]
&&vsp_add_13, // [13]
&&vsp_add_14, // [14]
&&vsp_add_15, // [15]
&&vsp_add_16, // [16]
&&vsp_add_17, // [17]
&&vsp_add_18, // [18]
&&vsp_add_19, // [19]
&&vsp_add_20, // [20]
&&vsp_add_21, // [21]
&&vsp_add_22, // [22]
&&vsp_add_23, // [23]
&&vsp_add_24, // [24]
&&vsp_add_25, // [25]
&&vsp_add_26, // [26]
&&vsp_add_27, // [27]
&&vsp_add_28, // [28]
&&vsp_add_29, // [29]
&&vsp_add_30, // [30]
&&vsp_add_31, // [31]
&&vsp_add_32, // [32]
&&vsp_add_33, // [33]
&&vsp_add_34, // [34]
&&vsp_add_35, // [35]
&&vsp_add_36, // [36]
&&vsp_add_37, // [37]
&&vsp_add_38, // [38]
&&vsp_add_39, // [39]
&&vsp_add_40, // [40]
&&vsp_add_41, // [41]
&&vsp_add_42, // [42]
&&vsp_add_43, // [43]
&&vsp_add_44, // [44]
&&vsp_add_45, // [45]
&&vsp_add_46, // [46]
&&vsp_add_47, // [47]
&&vsp_add_48, // [48]
&&vsp_add_49, // [49]
&&vsp_add_50, // [50]
&&vsp_add_51, // [51]
&&vsp_add_52, // [52]
&&vsp_add_53, // [53]
&&vsp_add_54, // [54]
&&vsp_add_55, // [55]
&&vsp_add_56, // [56]
&&vsp_add_57, // [57]
&&vsp_add_58, // [58]
&&vsp_add_59, // [59]
&&vsp_add_60, // [60]
&&vsp_add_61, // [61]
&&vsp_add_62, // [62]
&&vsp_add_63, // [63]
&&vsp_sub_0, // [64]
&&vsp_sub_1, // [65]
&&vsp_sub_2, // [66]
&&vsp_sub_3, // [67]
&&vsp_sub_4, // [68]
&&vsp_sub_5, // [69]
&&vsp_sub_6, // [70]
&&vsp_sub_7, // [71]
&&vsp_sub_8, // [72]
&&vsp_sub_9, // [73]
&&vsp_sub_10, // [74]
&&vsp_sub_11, // [75]
&&vsp_sub_12, // [76]
&&vsp_sub_13, // [77]
&&vsp_sub_14, // [78]
&&vsp_sub_15, // [79]
&&vsp_sub_16, // [80]
&&vsp_sub_17, // [81]
&&vsp_sub_18, // [82]
&&vsp_sub_19, // [83]
&&vsp_sub_20, // [84]
&&vsp_sub_21, // [85]
&&vsp_sub_22, // [86]
&&vsp_sub_23, // [87]
&&vsp_sub_24, // [88]
&&vsp_sub_25, // [89]
&&vsp_sub_26, // [90]
&&vsp_sub_27, // [91]
&&vsp_sub_28, // [92]
&&vsp_sub_29, // [93]
&&vsp_sub_30, // [94]
&&vsp_sub_31, // [95]
&&vsp_sub_32, // [96]
&&vsp_sub_33, // [97]
&&vsp_sub_34, // [98]
&&vsp_sub_35, // [99]
&&vsp_sub_36, // [100]
&&vsp_sub_37, // [101]
&&vsp_sub_38, // [102]
&&vsp_sub_39, // [103]
&&vsp_sub_40, // [104]
&&vsp_sub_41, // [105]
&&vsp_sub_42, // [106]
&&vsp_sub_43, // [107]
&&vsp_sub_44, // [108]
&&vsp_sub_45, // [109]
&&vsp_sub_46, // [110]
&&vsp_sub_47, // [111]
&&vsp_sub_48, // [112]
&&vsp_sub_49, // [113]
&&vsp_sub_50, // [114]
&&vsp_sub_51, // [115]
&&vsp_sub_52, // [116]
&&vsp_sub_53, // [117]
&&vsp_sub_54, // [118]
&&vsp_sub_55, // [119]
&&vsp_sub_56, // [120]
&&vsp_sub_57, // [121]
&&vsp_sub_58, // [122]
&&vsp_sub_59, // [123]
&&vsp_sub_60, // [124]
&&vsp_sub_61, // [125]
&&vsp_sub_62, // [126]
&&vsp_sub_63, // [127]
// 10000000
&&refuse_unwind_or_pop, // [0b1000'0000 = 128]
// 1000iiii ...
&&pop_under_mask, // [0b1000'0001 = 129]
&&pop_under_mask, // [0b1000'0010 = 130]
&&pop_under_mask, // [0b1000'0011 = 131]
&&pop_under_mask, // [0b1000'0100 = 132]
&&pop_under_mask, // [0b1000'0101 = 133]
&&pop_under_mask, // [0b1000'0110 = 134]
&&pop_under_mask, // [0b1000'0111 = 135]
&&pop_under_mask, // [0b1000'1000 = 136]
&&pop_under_mask, // [0b1000'1001 = 137]
&&pop_under_mask, // [0b1000'1010 = 138]
&&pop_under_mask, // [0b1000'1011 = 139]
&&pop_under_mask, // [0b1000'1100 = 140]
&&pop_under_mask, // [0b1000'1101 = 141]
&&pop_under_mask, // [0b1000'1110 = 142]
&&pop_under_mask, // [0b1000'1111 = 143] (128 + 15)