Space to Tab

This commit is contained in:
secXsQuared 2018-02-17 23:06:57 -05:00
parent 362f3d0ce8
commit 0988ae8baf
40 changed files with 3655 additions and 3171 deletions

View File

@ -8,36 +8,40 @@
static void KABI halp_obtain_cpu_info(boot_info_t *hal_info)
{
if(hal_info == NULL)
return;
uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
hal_cpuid(&eax,&ebx,&ecx,&edx);
lb_mem_copy(&ebx, &hal_info->cpu_vd_str[0], sizeof(uint32_t));
lb_mem_copy(&edx, &hal_info->cpu_vd_str[4], sizeof(uint32_t));
lb_mem_copy(&ecx, &hal_info->cpu_vd_str[8], sizeof(uint32_t));
hal_info->cpu_vd_str[12] = 0;
if (hal_info == NULL)
{
return;
}
uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
hal_cpuid(&eax, &ebx, &ecx, &edx);
lb_mem_copy(&ebx, &hal_info->cpu_vd_str[0], sizeof(uint32_t));
lb_mem_copy(&edx, &hal_info->cpu_vd_str[4], sizeof(uint32_t));
lb_mem_copy(&ecx, &hal_info->cpu_vd_str[8], sizeof(uint32_t));
hal_info->cpu_vd_str[12] = 0;
}
status_t KABI hal_init(void *m_info)
{
if (m_info == NULL || (uint64_t) m_info & lb_bit_field_mask(0, 2))
return STATUS_FAIL;
if (m_info == NULL || (uint64_t) m_info & lb_bit_field_mask(0, 2))
{
return STATUS_FAIL;
}
// init HAL infrastructures
hal_print_init();
hal_mem_init();
// init HAL infrastructures
hal_print_init();
hal_mem_init();
boot_info_t* boot_info = halloc(sizeof(boot_info_t));
boot_info_t *boot_info = halloc(sizeof(boot_info_t));
// obtain cpu info
halp_obtain_cpu_info(boot_info);
// obtain cpu info
halp_obtain_cpu_info(boot_info);
// init interrupt
if(hal_interrupt_init() != 0)
{
return STATUS_FAIL;
}
// init interrupt
if (hal_interrupt_init() != 0)
{
return STATUS_FAIL;
}
return STATUS_SUCCESS;
return STATUS_SUCCESS;
}

View File

@ -6,23 +6,23 @@
#include "hal/mem.h"
#include "lib/sxtdlib.h"
static uint8_t _idts[HAL_CORE_COUNT][IDT_ENTRY_NUM*IDT_ENTRY_SIZE];
static uint8_t _idts[HAL_CORE_COUNT][IDT_ENTRY_NUM * IDT_ENTRY_SIZE];
static hal_idt_ptr_t _idt_ptrs[HAL_CORE_COUNT];
static intr_handler_t _intr_handler_table[HAL_CORE_COUNT][IDT_ENTRY_NUM];
static void* _intr_handler_context_table[HAL_CORE_COUNT][IDT_ENTRY_NUM];
static void *_intr_handler_context_table[HAL_CORE_COUNT][IDT_ENTRY_NUM];
static exc_handler_t _exc_handler_table[HAL_CORE_COUNT][IDT_ENTRY_NUM];
irql_t KABI hal_set_irql(irql_t irql)
{
UNREFERENCED(irql)
hal_assert(false,"Unimplemented function called.");
return 0;
UNREFERENCED(irql)
hal_assert(false, "Unimplemented function called.");
return 0;
}
irql_t KABI hal_get_irql(void)
{
hal_assert(false,"Unimplemented function called.");
return 0;
hal_assert(false, "Unimplemented function called.");
return 0;
}
@ -31,432 +31,432 @@ void KABI hal_write_gate(void *const gate,
uint32_t const selector,
uint32_t const attr)
{
((uint8_t *) gate)[0] = (uint8_t) (offset & 0xFF);
((uint8_t *) gate)[1] = (uint8_t) ((offset >> 8) & 0xFF);
((uint8_t *) gate)[2] = (uint8_t) (selector & 0xFF);
((uint8_t *) gate)[3] = (uint8_t) ((selector >> 8) & 0xFF);
((uint8_t *) gate)[4] = (uint8_t) (attr & 0xFF);
((uint8_t *) gate)[5] = (uint8_t) ((attr >> 8) & 0xFF);
((uint8_t *) gate)[6] = (uint8_t) ((offset >> 16) & 0xFF);
((uint8_t *) gate)[7] = (uint8_t) ((offset >> 24) & 0xFF);
((uint8_t *) gate)[8] = (uint8_t) ((offset >> 32) & 0xFF);
((uint8_t *) gate)[9] = (uint8_t) ((offset >> 40) & 0xFF);
((uint8_t *) gate)[10] = (uint8_t) ((offset >> 48) & 0xFF);
((uint8_t *) gate)[11] = (uint8_t) ((offset >> 56) & 0xFF);
((uint8_t *) gate)[12] = 0;
((uint8_t *) gate)[13] = 0;
((uint8_t *) gate)[14] = 0;
((uint8_t *) gate)[15] = 0;
return;
((uint8_t *) gate)[0] = (uint8_t) (offset & 0xFF);
((uint8_t *) gate)[1] = (uint8_t) ((offset >> 8) & 0xFF);
((uint8_t *) gate)[2] = (uint8_t) (selector & 0xFF);
((uint8_t *) gate)[3] = (uint8_t) ((selector >> 8) & 0xFF);
((uint8_t *) gate)[4] = (uint8_t) (attr & 0xFF);
((uint8_t *) gate)[5] = (uint8_t) ((attr >> 8) & 0xFF);
((uint8_t *) gate)[6] = (uint8_t) ((offset >> 16) & 0xFF);
((uint8_t *) gate)[7] = (uint8_t) ((offset >> 24) & 0xFF);
((uint8_t *) gate)[8] = (uint8_t) ((offset >> 32) & 0xFF);
((uint8_t *) gate)[9] = (uint8_t) ((offset >> 40) & 0xFF);
((uint8_t *) gate)[10] = (uint8_t) ((offset >> 48) & 0xFF);
((uint8_t *) gate)[11] = (uint8_t) ((offset >> 56) & 0xFF);
((uint8_t *) gate)[12] = 0;
((uint8_t *) gate)[13] = 0;
((uint8_t *) gate)[14] = 0;
((uint8_t *) gate)[15] = 0;
return;
}
void KABI hal_set_interrupt_handler(uint64_t index,
void (*handler)(void))
{
if (index < IDT_ENTRY_NUM)
{
hal_write_gate(_idts[hal_get_core_id()] + 16 * index, (uint64_t) handler, seg_selector(1, 0),
GATE_DPL_0 | GATE_PRESENT | GATE_TYPE_INTERRUPT);
}
return;
if (index < IDT_ENTRY_NUM)
{
hal_write_gate(_idts[hal_get_core_id()] + 16 * index, (uint64_t) handler, seg_selector(1, 0),
GATE_DPL_0 | GATE_PRESENT | GATE_TYPE_INTERRUPT);
}
return;
}
void KABI hal_issue_interrupt(uint32_t target_core, uint32_t vector)
{
UNREFERENCED(target_core);
UNREFERENCED(vector);
hal_assert(false,"Unimplemented function called.");
return;
UNREFERENCED(target_core);
UNREFERENCED(vector);
hal_assert(false, "Unimplemented function called.");
return;
}
void KABI hal_register_interrupt_handler(uint32_t coreid, uint32_t index, intr_handler_t handler, void* context)
void KABI hal_register_interrupt_handler(uint32_t coreid, uint32_t index, intr_handler_t handler, void *context)
{
if (index < IDT_ENTRY_NUM && coreid < HAL_CORE_COUNT)
{
_intr_handler_table[coreid][index] = handler;
_intr_handler_context_table[coreid][index] = context;
}
return;
if (index < IDT_ENTRY_NUM && coreid < HAL_CORE_COUNT)
{
_intr_handler_table[coreid][index] = handler;
_intr_handler_context_table[coreid][index] = context;
}
return;
}
void KABI hal_deregister_interrupt_handler(uint32_t coreid, uint32_t index)
{
if (index < IDT_ENTRY_NUM && coreid < HAL_CORE_COUNT)
{
_intr_handler_table[coreid][index] = NULL;
}
return;
if (index < IDT_ENTRY_NUM && coreid < HAL_CORE_COUNT)
{
_intr_handler_table[coreid][index] = NULL;
}
return;
}
void KABI hal_register_exception_handler(uint32_t coreid, uint32_t index, exc_handler_t handler)
{
if (index < IDT_ENTRY_NUM && coreid < HAL_CORE_COUNT)
{
_exc_handler_table[coreid][index] = handler;
}
return;
if (index < IDT_ENTRY_NUM && coreid < HAL_CORE_COUNT)
{
_exc_handler_table[coreid][index] = handler;
}
return;
}
void KABI hal_deregister_exception_handler(uint32_t coreid, uint32_t index)
{
if (index < IDT_ENTRY_NUM && coreid < HAL_CORE_COUNT)
{
_exc_handler_table[coreid][index] = NULL;
}
return;
if (index < IDT_ENTRY_NUM && coreid < HAL_CORE_COUNT)
{
_exc_handler_table[coreid][index] = NULL;
}
return;
}
void KABI hal_interrupt_dispatcher(uint64_t int_vec, hal_interrupt_context_t *context)
{
uint32_t coreid = hal_get_core_id();
if (_intr_handler_table[int_vec] == NULL)
{
hal_printf("Unhandled interrupt %d at 0x%X.\n", int_vec, context->rip);
}
else
{
_intr_handler_table[coreid][int_vec](context, _intr_handler_context_table[coreid][int_vec]);
}
return;
uint32_t coreid = hal_get_core_id();
if (_intr_handler_table[int_vec] == NULL)
{
hal_printf("Unhandled interrupt %d at 0x%X.\n", int_vec, context->rip);
}
else
{
_intr_handler_table[coreid][int_vec](context, _intr_handler_context_table[coreid][int_vec]);
}
return;
}
void KABI hal_exception_dispatcher(uint64_t exc_vec, hal_interrupt_context_t* context, uint64_t errorcode)
void KABI hal_exception_dispatcher(uint64_t exc_vec, hal_interrupt_context_t *context, uint64_t errorcode)
{
uint32_t coreid = hal_get_core_id();
if (_exc_handler_table[exc_vec] == NULL)
{
hal_printf("Unhandled exception %d at 0x%X.\n", exc_vec, context->rip);
}
else
{
_exc_handler_table[coreid][exc_vec](context->rip, context->rsp, errorcode);
}
return;
uint32_t coreid = hal_get_core_id();
if (_exc_handler_table[exc_vec] == NULL)
{
hal_printf("Unhandled exception %d at 0x%X.\n", exc_vec, context->rip);
}
else
{
_exc_handler_table[coreid][exc_vec](context->rip, context->rsp, errorcode);
}
return;
}
static void KABI halp_populate_idt(void)
{
hal_set_interrupt_handler(0, hal_interrupt_handler_0);
hal_set_interrupt_handler(1, hal_interrupt_handler_1);
hal_set_interrupt_handler(2, hal_interrupt_handler_2);
hal_set_interrupt_handler(3, hal_interrupt_handler_3);
hal_set_interrupt_handler(4, hal_interrupt_handler_4);
hal_set_interrupt_handler(5, hal_interrupt_handler_5);
hal_set_interrupt_handler(6, hal_interrupt_handler_6);
hal_set_interrupt_handler(7, hal_interrupt_handler_7);
hal_set_interrupt_handler(8, hal_interrupt_handler_8);
hal_set_interrupt_handler(9, hal_interrupt_handler_9);
hal_set_interrupt_handler(10, hal_interrupt_handler_10);
hal_set_interrupt_handler(11, hal_interrupt_handler_11);
hal_set_interrupt_handler(12, hal_interrupt_handler_12);
hal_set_interrupt_handler(13, hal_interrupt_handler_13);
hal_set_interrupt_handler(14, hal_interrupt_handler_14);
hal_set_interrupt_handler(15, hal_interrupt_handler_15);
hal_set_interrupt_handler(16, hal_interrupt_handler_16);
hal_set_interrupt_handler(17, hal_interrupt_handler_17);
hal_set_interrupt_handler(18, hal_interrupt_handler_18);
hal_set_interrupt_handler(19, hal_interrupt_handler_19);
hal_set_interrupt_handler(20, hal_interrupt_handler_20);
hal_set_interrupt_handler(21, hal_interrupt_handler_21);
hal_set_interrupt_handler(22, hal_interrupt_handler_22);
hal_set_interrupt_handler(23, hal_interrupt_handler_23);
hal_set_interrupt_handler(24, hal_interrupt_handler_24);
hal_set_interrupt_handler(25, hal_interrupt_handler_25);
hal_set_interrupt_handler(26, hal_interrupt_handler_26);
hal_set_interrupt_handler(27, hal_interrupt_handler_27);
hal_set_interrupt_handler(28, hal_interrupt_handler_28);
hal_set_interrupt_handler(29, hal_interrupt_handler_29);
hal_set_interrupt_handler(30, hal_interrupt_handler_30);
hal_set_interrupt_handler(31, hal_interrupt_handler_31);
hal_set_interrupt_handler(32, hal_interrupt_handler_32);
hal_set_interrupt_handler(33, hal_interrupt_handler_33);
hal_set_interrupt_handler(34, hal_interrupt_handler_34);
hal_set_interrupt_handler(35, hal_interrupt_handler_35);
hal_set_interrupt_handler(36, hal_interrupt_handler_36);
hal_set_interrupt_handler(37, hal_interrupt_handler_37);
hal_set_interrupt_handler(38, hal_interrupt_handler_38);
hal_set_interrupt_handler(39, hal_interrupt_handler_39);
hal_set_interrupt_handler(40, hal_interrupt_handler_40);
hal_set_interrupt_handler(41, hal_interrupt_handler_41);
hal_set_interrupt_handler(42, hal_interrupt_handler_42);
hal_set_interrupt_handler(43, hal_interrupt_handler_43);
hal_set_interrupt_handler(44, hal_interrupt_handler_44);
hal_set_interrupt_handler(45, hal_interrupt_handler_45);
hal_set_interrupt_handler(46, hal_interrupt_handler_46);
hal_set_interrupt_handler(47, hal_interrupt_handler_47);
hal_set_interrupt_handler(48, hal_interrupt_handler_48);
hal_set_interrupt_handler(49, hal_interrupt_handler_49);
hal_set_interrupt_handler(50, hal_interrupt_handler_50);
hal_set_interrupt_handler(51, hal_interrupt_handler_51);
hal_set_interrupt_handler(52, hal_interrupt_handler_52);
hal_set_interrupt_handler(53, hal_interrupt_handler_53);
hal_set_interrupt_handler(54, hal_interrupt_handler_54);
hal_set_interrupt_handler(55, hal_interrupt_handler_55);
hal_set_interrupt_handler(56, hal_interrupt_handler_56);
hal_set_interrupt_handler(57, hal_interrupt_handler_57);
hal_set_interrupt_handler(58, hal_interrupt_handler_58);
hal_set_interrupt_handler(59, hal_interrupt_handler_59);
hal_set_interrupt_handler(60, hal_interrupt_handler_60);
hal_set_interrupt_handler(61, hal_interrupt_handler_61);
hal_set_interrupt_handler(62, hal_interrupt_handler_62);
hal_set_interrupt_handler(63, hal_interrupt_handler_63);
hal_set_interrupt_handler(64, hal_interrupt_handler_64);
hal_set_interrupt_handler(65, hal_interrupt_handler_65);
hal_set_interrupt_handler(66, hal_interrupt_handler_66);
hal_set_interrupt_handler(67, hal_interrupt_handler_67);
hal_set_interrupt_handler(68, hal_interrupt_handler_68);
hal_set_interrupt_handler(69, hal_interrupt_handler_69);
hal_set_interrupt_handler(70, hal_interrupt_handler_70);
hal_set_interrupt_handler(71, hal_interrupt_handler_71);
hal_set_interrupt_handler(72, hal_interrupt_handler_72);
hal_set_interrupt_handler(73, hal_interrupt_handler_73);
hal_set_interrupt_handler(74, hal_interrupt_handler_74);
hal_set_interrupt_handler(75, hal_interrupt_handler_75);
hal_set_interrupt_handler(76, hal_interrupt_handler_76);
hal_set_interrupt_handler(77, hal_interrupt_handler_77);
hal_set_interrupt_handler(78, hal_interrupt_handler_78);
hal_set_interrupt_handler(79, hal_interrupt_handler_79);
hal_set_interrupt_handler(80, hal_interrupt_handler_80);
hal_set_interrupt_handler(81, hal_interrupt_handler_81);
hal_set_interrupt_handler(82, hal_interrupt_handler_82);
hal_set_interrupt_handler(83, hal_interrupt_handler_83);
hal_set_interrupt_handler(84, hal_interrupt_handler_84);
hal_set_interrupt_handler(85, hal_interrupt_handler_85);
hal_set_interrupt_handler(86, hal_interrupt_handler_86);
hal_set_interrupt_handler(87, hal_interrupt_handler_87);
hal_set_interrupt_handler(88, hal_interrupt_handler_88);
hal_set_interrupt_handler(89, hal_interrupt_handler_89);
hal_set_interrupt_handler(90, hal_interrupt_handler_90);
hal_set_interrupt_handler(91, hal_interrupt_handler_91);
hal_set_interrupt_handler(92, hal_interrupt_handler_92);
hal_set_interrupt_handler(93, hal_interrupt_handler_93);
hal_set_interrupt_handler(94, hal_interrupt_handler_94);
hal_set_interrupt_handler(95, hal_interrupt_handler_95);
hal_set_interrupt_handler(96, hal_interrupt_handler_96);
hal_set_interrupt_handler(97, hal_interrupt_handler_97);
hal_set_interrupt_handler(98, hal_interrupt_handler_98);
hal_set_interrupt_handler(99, hal_interrupt_handler_99);
hal_set_interrupt_handler(100, hal_interrupt_handler_100);
hal_set_interrupt_handler(101, hal_interrupt_handler_101);
hal_set_interrupt_handler(102, hal_interrupt_handler_102);
hal_set_interrupt_handler(103, hal_interrupt_handler_103);
hal_set_interrupt_handler(104, hal_interrupt_handler_104);
hal_set_interrupt_handler(105, hal_interrupt_handler_105);
hal_set_interrupt_handler(106, hal_interrupt_handler_106);
hal_set_interrupt_handler(107, hal_interrupt_handler_107);
hal_set_interrupt_handler(108, hal_interrupt_handler_108);
hal_set_interrupt_handler(109, hal_interrupt_handler_109);
hal_set_interrupt_handler(110, hal_interrupt_handler_110);
hal_set_interrupt_handler(111, hal_interrupt_handler_111);
hal_set_interrupt_handler(112, hal_interrupt_handler_112);
hal_set_interrupt_handler(113, hal_interrupt_handler_113);
hal_set_interrupt_handler(114, hal_interrupt_handler_114);
hal_set_interrupt_handler(115, hal_interrupt_handler_115);
hal_set_interrupt_handler(116, hal_interrupt_handler_116);
hal_set_interrupt_handler(117, hal_interrupt_handler_117);
hal_set_interrupt_handler(118, hal_interrupt_handler_118);
hal_set_interrupt_handler(119, hal_interrupt_handler_119);
hal_set_interrupt_handler(120, hal_interrupt_handler_120);
hal_set_interrupt_handler(121, hal_interrupt_handler_121);
hal_set_interrupt_handler(122, hal_interrupt_handler_122);
hal_set_interrupt_handler(123, hal_interrupt_handler_123);
hal_set_interrupt_handler(124, hal_interrupt_handler_124);
hal_set_interrupt_handler(125, hal_interrupt_handler_125);
hal_set_interrupt_handler(126, hal_interrupt_handler_126);
hal_set_interrupt_handler(127, hal_interrupt_handler_127);
hal_set_interrupt_handler(128, hal_interrupt_handler_128);
hal_set_interrupt_handler(129, hal_interrupt_handler_129);
hal_set_interrupt_handler(130, hal_interrupt_handler_130);
hal_set_interrupt_handler(131, hal_interrupt_handler_131);
hal_set_interrupt_handler(132, hal_interrupt_handler_132);
hal_set_interrupt_handler(133, hal_interrupt_handler_133);
hal_set_interrupt_handler(134, hal_interrupt_handler_134);
hal_set_interrupt_handler(135, hal_interrupt_handler_135);
hal_set_interrupt_handler(136, hal_interrupt_handler_136);
hal_set_interrupt_handler(137, hal_interrupt_handler_137);
hal_set_interrupt_handler(138, hal_interrupt_handler_138);
hal_set_interrupt_handler(139, hal_interrupt_handler_139);
hal_set_interrupt_handler(140, hal_interrupt_handler_140);
hal_set_interrupt_handler(141, hal_interrupt_handler_141);
hal_set_interrupt_handler(142, hal_interrupt_handler_142);
hal_set_interrupt_handler(143, hal_interrupt_handler_143);
hal_set_interrupt_handler(144, hal_interrupt_handler_144);
hal_set_interrupt_handler(145, hal_interrupt_handler_145);
hal_set_interrupt_handler(146, hal_interrupt_handler_146);
hal_set_interrupt_handler(147, hal_interrupt_handler_147);
hal_set_interrupt_handler(148, hal_interrupt_handler_148);
hal_set_interrupt_handler(149, hal_interrupt_handler_149);
hal_set_interrupt_handler(150, hal_interrupt_handler_150);
hal_set_interrupt_handler(151, hal_interrupt_handler_151);
hal_set_interrupt_handler(152, hal_interrupt_handler_152);
hal_set_interrupt_handler(153, hal_interrupt_handler_153);
hal_set_interrupt_handler(154, hal_interrupt_handler_154);
hal_set_interrupt_handler(155, hal_interrupt_handler_155);
hal_set_interrupt_handler(156, hal_interrupt_handler_156);
hal_set_interrupt_handler(157, hal_interrupt_handler_157);
hal_set_interrupt_handler(158, hal_interrupt_handler_158);
hal_set_interrupt_handler(159, hal_interrupt_handler_159);
hal_set_interrupt_handler(160, hal_interrupt_handler_160);
hal_set_interrupt_handler(161, hal_interrupt_handler_161);
hal_set_interrupt_handler(162, hal_interrupt_handler_162);
hal_set_interrupt_handler(163, hal_interrupt_handler_163);
hal_set_interrupt_handler(164, hal_interrupt_handler_164);
hal_set_interrupt_handler(165, hal_interrupt_handler_165);
hal_set_interrupt_handler(166, hal_interrupt_handler_166);
hal_set_interrupt_handler(167, hal_interrupt_handler_167);
hal_set_interrupt_handler(168, hal_interrupt_handler_168);
hal_set_interrupt_handler(169, hal_interrupt_handler_169);
hal_set_interrupt_handler(170, hal_interrupt_handler_170);
hal_set_interrupt_handler(171, hal_interrupt_handler_171);
hal_set_interrupt_handler(172, hal_interrupt_handler_172);
hal_set_interrupt_handler(173, hal_interrupt_handler_173);
hal_set_interrupt_handler(174, hal_interrupt_handler_174);
hal_set_interrupt_handler(175, hal_interrupt_handler_175);
hal_set_interrupt_handler(176, hal_interrupt_handler_176);
hal_set_interrupt_handler(177, hal_interrupt_handler_177);
hal_set_interrupt_handler(178, hal_interrupt_handler_178);
hal_set_interrupt_handler(179, hal_interrupt_handler_179);
hal_set_interrupt_handler(180, hal_interrupt_handler_180);
hal_set_interrupt_handler(181, hal_interrupt_handler_181);
hal_set_interrupt_handler(182, hal_interrupt_handler_182);
hal_set_interrupt_handler(183, hal_interrupt_handler_183);
hal_set_interrupt_handler(184, hal_interrupt_handler_184);
hal_set_interrupt_handler(185, hal_interrupt_handler_185);
hal_set_interrupt_handler(186, hal_interrupt_handler_186);
hal_set_interrupt_handler(187, hal_interrupt_handler_187);
hal_set_interrupt_handler(188, hal_interrupt_handler_188);
hal_set_interrupt_handler(189, hal_interrupt_handler_189);
hal_set_interrupt_handler(190, hal_interrupt_handler_190);
hal_set_interrupt_handler(191, hal_interrupt_handler_191);
hal_set_interrupt_handler(192, hal_interrupt_handler_192);
hal_set_interrupt_handler(193, hal_interrupt_handler_193);
hal_set_interrupt_handler(194, hal_interrupt_handler_194);
hal_set_interrupt_handler(195, hal_interrupt_handler_195);
hal_set_interrupt_handler(196, hal_interrupt_handler_196);
hal_set_interrupt_handler(197, hal_interrupt_handler_197);
hal_set_interrupt_handler(198, hal_interrupt_handler_198);
hal_set_interrupt_handler(199, hal_interrupt_handler_199);
hal_set_interrupt_handler(200, hal_interrupt_handler_200);
hal_set_interrupt_handler(201, hal_interrupt_handler_201);
hal_set_interrupt_handler(202, hal_interrupt_handler_202);
hal_set_interrupt_handler(203, hal_interrupt_handler_203);
hal_set_interrupt_handler(204, hal_interrupt_handler_204);
hal_set_interrupt_handler(205, hal_interrupt_handler_205);
hal_set_interrupt_handler(206, hal_interrupt_handler_206);
hal_set_interrupt_handler(207, hal_interrupt_handler_207);
hal_set_interrupt_handler(208, hal_interrupt_handler_208);
hal_set_interrupt_handler(209, hal_interrupt_handler_209);
hal_set_interrupt_handler(210, hal_interrupt_handler_210);
hal_set_interrupt_handler(211, hal_interrupt_handler_211);
hal_set_interrupt_handler(212, hal_interrupt_handler_212);
hal_set_interrupt_handler(213, hal_interrupt_handler_213);
hal_set_interrupt_handler(214, hal_interrupt_handler_214);
hal_set_interrupt_handler(215, hal_interrupt_handler_215);
hal_set_interrupt_handler(216, hal_interrupt_handler_216);
hal_set_interrupt_handler(217, hal_interrupt_handler_217);
hal_set_interrupt_handler(218, hal_interrupt_handler_218);
hal_set_interrupt_handler(219, hal_interrupt_handler_219);
hal_set_interrupt_handler(220, hal_interrupt_handler_220);
hal_set_interrupt_handler(221, hal_interrupt_handler_221);
hal_set_interrupt_handler(222, hal_interrupt_handler_222);
hal_set_interrupt_handler(223, hal_interrupt_handler_223);
hal_set_interrupt_handler(224, hal_interrupt_handler_224);
hal_set_interrupt_handler(225, hal_interrupt_handler_225);
hal_set_interrupt_handler(226, hal_interrupt_handler_226);
hal_set_interrupt_handler(227, hal_interrupt_handler_227);
hal_set_interrupt_handler(228, hal_interrupt_handler_228);
hal_set_interrupt_handler(229, hal_interrupt_handler_229);
hal_set_interrupt_handler(230, hal_interrupt_handler_230);
hal_set_interrupt_handler(231, hal_interrupt_handler_231);
hal_set_interrupt_handler(232, hal_interrupt_handler_232);
hal_set_interrupt_handler(233, hal_interrupt_handler_233);
hal_set_interrupt_handler(234, hal_interrupt_handler_234);
hal_set_interrupt_handler(235, hal_interrupt_handler_235);
hal_set_interrupt_handler(236, hal_interrupt_handler_236);
hal_set_interrupt_handler(237, hal_interrupt_handler_237);
hal_set_interrupt_handler(238, hal_interrupt_handler_238);
hal_set_interrupt_handler(239, hal_interrupt_handler_239);
hal_set_interrupt_handler(240, hal_interrupt_handler_240);
hal_set_interrupt_handler(241, hal_interrupt_handler_241);
hal_set_interrupt_handler(242, hal_interrupt_handler_242);
hal_set_interrupt_handler(243, hal_interrupt_handler_243);
hal_set_interrupt_handler(244, hal_interrupt_handler_244);
hal_set_interrupt_handler(245, hal_interrupt_handler_245);
hal_set_interrupt_handler(246, hal_interrupt_handler_246);
hal_set_interrupt_handler(247, hal_interrupt_handler_247);
hal_set_interrupt_handler(248, hal_interrupt_handler_248);
hal_set_interrupt_handler(249, hal_interrupt_handler_249);
hal_set_interrupt_handler(250, hal_interrupt_handler_250);
hal_set_interrupt_handler(251, hal_interrupt_handler_251);
hal_set_interrupt_handler(252, hal_interrupt_handler_252);
hal_set_interrupt_handler(253, hal_interrupt_handler_253);
hal_set_interrupt_handler(254, hal_interrupt_handler_254);
hal_set_interrupt_handler(255, hal_interrupt_handler_255);
return;
hal_set_interrupt_handler(0, hal_interrupt_handler_0);
hal_set_interrupt_handler(1, hal_interrupt_handler_1);
hal_set_interrupt_handler(2, hal_interrupt_handler_2);
hal_set_interrupt_handler(3, hal_interrupt_handler_3);
hal_set_interrupt_handler(4, hal_interrupt_handler_4);
hal_set_interrupt_handler(5, hal_interrupt_handler_5);
hal_set_interrupt_handler(6, hal_interrupt_handler_6);
hal_set_interrupt_handler(7, hal_interrupt_handler_7);
hal_set_interrupt_handler(8, hal_interrupt_handler_8);
hal_set_interrupt_handler(9, hal_interrupt_handler_9);
hal_set_interrupt_handler(10, hal_interrupt_handler_10);
hal_set_interrupt_handler(11, hal_interrupt_handler_11);
hal_set_interrupt_handler(12, hal_interrupt_handler_12);
hal_set_interrupt_handler(13, hal_interrupt_handler_13);
hal_set_interrupt_handler(14, hal_interrupt_handler_14);
hal_set_interrupt_handler(15, hal_interrupt_handler_15);
hal_set_interrupt_handler(16, hal_interrupt_handler_16);
hal_set_interrupt_handler(17, hal_interrupt_handler_17);
hal_set_interrupt_handler(18, hal_interrupt_handler_18);
hal_set_interrupt_handler(19, hal_interrupt_handler_19);
hal_set_interrupt_handler(20, hal_interrupt_handler_20);
hal_set_interrupt_handler(21, hal_interrupt_handler_21);
hal_set_interrupt_handler(22, hal_interrupt_handler_22);
hal_set_interrupt_handler(23, hal_interrupt_handler_23);
hal_set_interrupt_handler(24, hal_interrupt_handler_24);
hal_set_interrupt_handler(25, hal_interrupt_handler_25);
hal_set_interrupt_handler(26, hal_interrupt_handler_26);
hal_set_interrupt_handler(27, hal_interrupt_handler_27);
hal_set_interrupt_handler(28, hal_interrupt_handler_28);
hal_set_interrupt_handler(29, hal_interrupt_handler_29);
hal_set_interrupt_handler(30, hal_interrupt_handler_30);
hal_set_interrupt_handler(31, hal_interrupt_handler_31);
hal_set_interrupt_handler(32, hal_interrupt_handler_32);
hal_set_interrupt_handler(33, hal_interrupt_handler_33);
hal_set_interrupt_handler(34, hal_interrupt_handler_34);
hal_set_interrupt_handler(35, hal_interrupt_handler_35);
hal_set_interrupt_handler(36, hal_interrupt_handler_36);
hal_set_interrupt_handler(37, hal_interrupt_handler_37);
hal_set_interrupt_handler(38, hal_interrupt_handler_38);
hal_set_interrupt_handler(39, hal_interrupt_handler_39);
hal_set_interrupt_handler(40, hal_interrupt_handler_40);
hal_set_interrupt_handler(41, hal_interrupt_handler_41);
hal_set_interrupt_handler(42, hal_interrupt_handler_42);
hal_set_interrupt_handler(43, hal_interrupt_handler_43);
hal_set_interrupt_handler(44, hal_interrupt_handler_44);
hal_set_interrupt_handler(45, hal_interrupt_handler_45);
hal_set_interrupt_handler(46, hal_interrupt_handler_46);
hal_set_interrupt_handler(47, hal_interrupt_handler_47);
hal_set_interrupt_handler(48, hal_interrupt_handler_48);
hal_set_interrupt_handler(49, hal_interrupt_handler_49);
hal_set_interrupt_handler(50, hal_interrupt_handler_50);
hal_set_interrupt_handler(51, hal_interrupt_handler_51);
hal_set_interrupt_handler(52, hal_interrupt_handler_52);
hal_set_interrupt_handler(53, hal_interrupt_handler_53);
hal_set_interrupt_handler(54, hal_interrupt_handler_54);
hal_set_interrupt_handler(55, hal_interrupt_handler_55);
hal_set_interrupt_handler(56, hal_interrupt_handler_56);
hal_set_interrupt_handler(57, hal_interrupt_handler_57);
hal_set_interrupt_handler(58, hal_interrupt_handler_58);
hal_set_interrupt_handler(59, hal_interrupt_handler_59);
hal_set_interrupt_handler(60, hal_interrupt_handler_60);
hal_set_interrupt_handler(61, hal_interrupt_handler_61);
hal_set_interrupt_handler(62, hal_interrupt_handler_62);
hal_set_interrupt_handler(63, hal_interrupt_handler_63);
hal_set_interrupt_handler(64, hal_interrupt_handler_64);
hal_set_interrupt_handler(65, hal_interrupt_handler_65);
hal_set_interrupt_handler(66, hal_interrupt_handler_66);
hal_set_interrupt_handler(67, hal_interrupt_handler_67);
hal_set_interrupt_handler(68, hal_interrupt_handler_68);
hal_set_interrupt_handler(69, hal_interrupt_handler_69);
hal_set_interrupt_handler(70, hal_interrupt_handler_70);
hal_set_interrupt_handler(71, hal_interrupt_handler_71);
hal_set_interrupt_handler(72, hal_interrupt_handler_72);
hal_set_interrupt_handler(73, hal_interrupt_handler_73);
hal_set_interrupt_handler(74, hal_interrupt_handler_74);
hal_set_interrupt_handler(75, hal_interrupt_handler_75);
hal_set_interrupt_handler(76, hal_interrupt_handler_76);
hal_set_interrupt_handler(77, hal_interrupt_handler_77);
hal_set_interrupt_handler(78, hal_interrupt_handler_78);
hal_set_interrupt_handler(79, hal_interrupt_handler_79);
hal_set_interrupt_handler(80, hal_interrupt_handler_80);
hal_set_interrupt_handler(81, hal_interrupt_handler_81);
hal_set_interrupt_handler(82, hal_interrupt_handler_82);
hal_set_interrupt_handler(83, hal_interrupt_handler_83);
hal_set_interrupt_handler(84, hal_interrupt_handler_84);
hal_set_interrupt_handler(85, hal_interrupt_handler_85);
hal_set_interrupt_handler(86, hal_interrupt_handler_86);
hal_set_interrupt_handler(87, hal_interrupt_handler_87);
hal_set_interrupt_handler(88, hal_interrupt_handler_88);
hal_set_interrupt_handler(89, hal_interrupt_handler_89);
hal_set_interrupt_handler(90, hal_interrupt_handler_90);
hal_set_interrupt_handler(91, hal_interrupt_handler_91);
hal_set_interrupt_handler(92, hal_interrupt_handler_92);
hal_set_interrupt_handler(93, hal_interrupt_handler_93);
hal_set_interrupt_handler(94, hal_interrupt_handler_94);
hal_set_interrupt_handler(95, hal_interrupt_handler_95);
hal_set_interrupt_handler(96, hal_interrupt_handler_96);
hal_set_interrupt_handler(97, hal_interrupt_handler_97);
hal_set_interrupt_handler(98, hal_interrupt_handler_98);
hal_set_interrupt_handler(99, hal_interrupt_handler_99);
hal_set_interrupt_handler(100, hal_interrupt_handler_100);
hal_set_interrupt_handler(101, hal_interrupt_handler_101);
hal_set_interrupt_handler(102, hal_interrupt_handler_102);
hal_set_interrupt_handler(103, hal_interrupt_handler_103);
hal_set_interrupt_handler(104, hal_interrupt_handler_104);
hal_set_interrupt_handler(105, hal_interrupt_handler_105);
hal_set_interrupt_handler(106, hal_interrupt_handler_106);
hal_set_interrupt_handler(107, hal_interrupt_handler_107);
hal_set_interrupt_handler(108, hal_interrupt_handler_108);
hal_set_interrupt_handler(109, hal_interrupt_handler_109);
hal_set_interrupt_handler(110, hal_interrupt_handler_110);
hal_set_interrupt_handler(111, hal_interrupt_handler_111);
hal_set_interrupt_handler(112, hal_interrupt_handler_112);
hal_set_interrupt_handler(113, hal_interrupt_handler_113);
hal_set_interrupt_handler(114, hal_interrupt_handler_114);
hal_set_interrupt_handler(115, hal_interrupt_handler_115);
hal_set_interrupt_handler(116, hal_interrupt_handler_116);
hal_set_interrupt_handler(117, hal_interrupt_handler_117);
hal_set_interrupt_handler(118, hal_interrupt_handler_118);
hal_set_interrupt_handler(119, hal_interrupt_handler_119);
hal_set_interrupt_handler(120, hal_interrupt_handler_120);
hal_set_interrupt_handler(121, hal_interrupt_handler_121);
hal_set_interrupt_handler(122, hal_interrupt_handler_122);
hal_set_interrupt_handler(123, hal_interrupt_handler_123);
hal_set_interrupt_handler(124, hal_interrupt_handler_124);
hal_set_interrupt_handler(125, hal_interrupt_handler_125);
hal_set_interrupt_handler(126, hal_interrupt_handler_126);
hal_set_interrupt_handler(127, hal_interrupt_handler_127);
hal_set_interrupt_handler(128, hal_interrupt_handler_128);
hal_set_interrupt_handler(129, hal_interrupt_handler_129);
hal_set_interrupt_handler(130, hal_interrupt_handler_130);
hal_set_interrupt_handler(131, hal_interrupt_handler_131);
hal_set_interrupt_handler(132, hal_interrupt_handler_132);
hal_set_interrupt_handler(133, hal_interrupt_handler_133);
hal_set_interrupt_handler(134, hal_interrupt_handler_134);
hal_set_interrupt_handler(135, hal_interrupt_handler_135);
hal_set_interrupt_handler(136, hal_interrupt_handler_136);
hal_set_interrupt_handler(137, hal_interrupt_handler_137);
hal_set_interrupt_handler(138, hal_interrupt_handler_138);
hal_set_interrupt_handler(139, hal_interrupt_handler_139);
hal_set_interrupt_handler(140, hal_interrupt_handler_140);
hal_set_interrupt_handler(141, hal_interrupt_handler_141);
hal_set_interrupt_handler(142, hal_interrupt_handler_142);
hal_set_interrupt_handler(143, hal_interrupt_handler_143);
hal_set_interrupt_handler(144, hal_interrupt_handler_144);
hal_set_interrupt_handler(145, hal_interrupt_handler_145);
hal_set_interrupt_handler(146, hal_interrupt_handler_146);
hal_set_interrupt_handler(147, hal_interrupt_handler_147);
hal_set_interrupt_handler(148, hal_interrupt_handler_148);
hal_set_interrupt_handler(149, hal_interrupt_handler_149);
hal_set_interrupt_handler(150, hal_interrupt_handler_150);
hal_set_interrupt_handler(151, hal_interrupt_handler_151);
hal_set_interrupt_handler(152, hal_interrupt_handler_152);
hal_set_interrupt_handler(153, hal_interrupt_handler_153);
hal_set_interrupt_handler(154, hal_interrupt_handler_154);
hal_set_interrupt_handler(155, hal_interrupt_handler_155);
hal_set_interrupt_handler(156, hal_interrupt_handler_156);
hal_set_interrupt_handler(157, hal_interrupt_handler_157);
hal_set_interrupt_handler(158, hal_interrupt_handler_158);
hal_set_interrupt_handler(159, hal_interrupt_handler_159);
hal_set_interrupt_handler(160, hal_interrupt_handler_160);
hal_set_interrupt_handler(161, hal_interrupt_handler_161);
hal_set_interrupt_handler(162, hal_interrupt_handler_162);
hal_set_interrupt_handler(163, hal_interrupt_handler_163);
hal_set_interrupt_handler(164, hal_interrupt_handler_164);
hal_set_interrupt_handler(165, hal_interrupt_handler_165);
hal_set_interrupt_handler(166, hal_interrupt_handler_166);
hal_set_interrupt_handler(167, hal_interrupt_handler_167);
hal_set_interrupt_handler(168, hal_interrupt_handler_168);
hal_set_interrupt_handler(169, hal_interrupt_handler_169);
hal_set_interrupt_handler(170, hal_interrupt_handler_170);
hal_set_interrupt_handler(171, hal_interrupt_handler_171);
hal_set_interrupt_handler(172, hal_interrupt_handler_172);
hal_set_interrupt_handler(173, hal_interrupt_handler_173);
hal_set_interrupt_handler(174, hal_interrupt_handler_174);
hal_set_interrupt_handler(175, hal_interrupt_handler_175);
hal_set_interrupt_handler(176, hal_interrupt_handler_176);
hal_set_interrupt_handler(177, hal_interrupt_handler_177);
hal_set_interrupt_handler(178, hal_interrupt_handler_178);
hal_set_interrupt_handler(179, hal_interrupt_handler_179);
hal_set_interrupt_handler(180, hal_interrupt_handler_180);
hal_set_interrupt_handler(181, hal_interrupt_handler_181);
hal_set_interrupt_handler(182, hal_interrupt_handler_182);
hal_set_interrupt_handler(183, hal_interrupt_handler_183);
hal_set_interrupt_handler(184, hal_interrupt_handler_184);
hal_set_interrupt_handler(185, hal_interrupt_handler_185);
hal_set_interrupt_handler(186, hal_interrupt_handler_186);
hal_set_interrupt_handler(187, hal_interrupt_handler_187);
hal_set_interrupt_handler(188, hal_interrupt_handler_188);
hal_set_interrupt_handler(189, hal_interrupt_handler_189);
hal_set_interrupt_handler(190, hal_interrupt_handler_190);
hal_set_interrupt_handler(191, hal_interrupt_handler_191);
hal_set_interrupt_handler(192, hal_interrupt_handler_192);
hal_set_interrupt_handler(193, hal_interrupt_handler_193);
hal_set_interrupt_handler(194, hal_interrupt_handler_194);
hal_set_interrupt_handler(195, hal_interrupt_handler_195);
hal_set_interrupt_handler(196, hal_interrupt_handler_196);
hal_set_interrupt_handler(197, hal_interrupt_handler_197);
hal_set_interrupt_handler(198, hal_interrupt_handler_198);
hal_set_interrupt_handler(199, hal_interrupt_handler_199);
hal_set_interrupt_handler(200, hal_interrupt_handler_200);
hal_set_interrupt_handler(201, hal_interrupt_handler_201);
hal_set_interrupt_handler(202, hal_interrupt_handler_202);
hal_set_interrupt_handler(203, hal_interrupt_handler_203);
hal_set_interrupt_handler(204, hal_interrupt_handler_204);
hal_set_interrupt_handler(205, hal_interrupt_handler_205);
hal_set_interrupt_handler(206, hal_interrupt_handler_206);
hal_set_interrupt_handler(207, hal_interrupt_handler_207);
hal_set_interrupt_handler(208, hal_interrupt_handler_208);
hal_set_interrupt_handler(209, hal_interrupt_handler_209);
hal_set_interrupt_handler(210, hal_interrupt_handler_210);
hal_set_interrupt_handler(211, hal_interrupt_handler_211);
hal_set_interrupt_handler(212, hal_interrupt_handler_212);
hal_set_interrupt_handler(213, hal_interrupt_handler_213);
hal_set_interrupt_handler(214, hal_interrupt_handler_214);
hal_set_interrupt_handler(215, hal_interrupt_handler_215);
hal_set_interrupt_handler(216, hal_interrupt_handler_216);
hal_set_interrupt_handler(217, hal_interrupt_handler_217);
hal_set_interrupt_handler(218, hal_interrupt_handler_218);
hal_set_interrupt_handler(219, hal_interrupt_handler_219);
hal_set_interrupt_handler(220, hal_interrupt_handler_220);
hal_set_interrupt_handler(221, hal_interrupt_handler_221);
hal_set_interrupt_handler(222, hal_interrupt_handler_222);
hal_set_interrupt_handler(223, hal_interrupt_handler_223);
hal_set_interrupt_handler(224, hal_interrupt_handler_224);
hal_set_interrupt_handler(225, hal_interrupt_handler_225);
hal_set_interrupt_handler(226, hal_interrupt_handler_226);
hal_set_interrupt_handler(227, hal_interrupt_handler_227);
hal_set_interrupt_handler(228, hal_interrupt_handler_228);
hal_set_interrupt_handler(229, hal_interrupt_handler_229);
hal_set_interrupt_handler(230, hal_interrupt_handler_230);
hal_set_interrupt_handler(231, hal_interrupt_handler_231);
hal_set_interrupt_handler(232, hal_interrupt_handler_232);
hal_set_interrupt_handler(233, hal_interrupt_handler_233);
hal_set_interrupt_handler(234, hal_interrupt_handler_234);
hal_set_interrupt_handler(235, hal_interrupt_handler_235);
hal_set_interrupt_handler(236, hal_interrupt_handler_236);
hal_set_interrupt_handler(237, hal_interrupt_handler_237);
hal_set_interrupt_handler(238, hal_interrupt_handler_238);
hal_set_interrupt_handler(239, hal_interrupt_handler_239);
hal_set_interrupt_handler(240, hal_interrupt_handler_240);
hal_set_interrupt_handler(241, hal_interrupt_handler_241);
hal_set_interrupt_handler(242, hal_interrupt_handler_242);
hal_set_interrupt_handler(243, hal_interrupt_handler_243);
hal_set_interrupt_handler(244, hal_interrupt_handler_244);
hal_set_interrupt_handler(245, hal_interrupt_handler_245);
hal_set_interrupt_handler(246, hal_interrupt_handler_246);
hal_set_interrupt_handler(247, hal_interrupt_handler_247);
hal_set_interrupt_handler(248, hal_interrupt_handler_248);
hal_set_interrupt_handler(249, hal_interrupt_handler_249);
hal_set_interrupt_handler(250, hal_interrupt_handler_250);
hal_set_interrupt_handler(251, hal_interrupt_handler_251);
hal_set_interrupt_handler(252, hal_interrupt_handler_252);
hal_set_interrupt_handler(253, hal_interrupt_handler_253);
hal_set_interrupt_handler(254, hal_interrupt_handler_254);
hal_set_interrupt_handler(255, hal_interrupt_handler_255);
return;
}
uint32_t KABI hal_get_core_id(void)
{
// TODO
return 0;
// TODO
return 0;
}
int32_t KABI hal_interrupt_init(void)
{
uint32_t coreid = hal_get_core_id();
uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
eax = 1;
hal_cpuid(&eax, &ebx, &ecx, &edx);
if (!(edx & lb_bit_mask(9)))
{
hal_printf("ERROR: APIC not supported by CPU.\n");
return 1;
}
uint32_t coreid = hal_get_core_id();
uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
eax = 1;
hal_cpuid(&eax, &ebx, &ecx, &edx);
if (!(edx & lb_bit_mask(9)))
{
hal_printf("ERROR: APIC not supported by CPU.\n");
return 1;
}
// get idt ptr ready
_idt_ptrs[coreid].base = (uint64_t) &_idts[coreid];
_idt_ptrs[coreid].limit = IDT_ENTRY_NUM * IDT_ENTRY_SIZE - 1;
// get idt ptr ready
_idt_ptrs[coreid].base = (uint64_t) &_idts[coreid];
_idt_ptrs[coreid].limit = IDT_ENTRY_NUM * IDT_ENTRY_SIZE - 1;
// clear dispatch table
for (uint64_t i = 0; i < IDT_ENTRY_NUM; i++)
{
_intr_handler_table[coreid][i] = NULL;
_exc_handler_table[coreid][i] = NULL;
_intr_handler_context_table[coreid][i] = NULL;
}
// clear dispatch table
for (uint64_t i = 0; i < IDT_ENTRY_NUM; i++)
{
_intr_handler_table[coreid][i] = NULL;
_exc_handler_table[coreid][i] = NULL;
_intr_handler_context_table[coreid][i] = NULL;
}
// hook asm interrupt handlers
halp_populate_idt();
// hook asm interrupt handlers
halp_populate_idt();
hal_flush_idt(&_idt_ptrs[coreid]);
hal_flush_idt(&_idt_ptrs[coreid]);
// disable PIC
hal_write_port_8(0xa1, 0xff);
hal_write_port_8(0x21, 0xff);
// disable PIC
hal_write_port_8(0xa1, 0xff);
hal_write_port_8(0x21, 0xff);
uint64_t apic_base_reg = 0;
uint64_t apic_base = 0;
ecx = MSR_IA32_APIC_BASE;
hal_read_msr(&ecx, &edx, &eax);
apic_base_reg = ((uint64_t) edx << 32) + (uint64_t) eax;
apic_base = apic_base_reg & lb_bit_field_mask(12, 35);
UNREFERENCED(apic_base);
uint64_t apic_base_reg = 0;
uint64_t apic_base = 0;
ecx = MSR_IA32_APIC_BASE;
hal_read_msr(&ecx, &edx, &eax);
apic_base_reg = ((uint64_t) edx << 32) + (uint64_t) eax;
apic_base = apic_base_reg & lb_bit_field_mask(12, 35);
UNREFERENCED(apic_base);
//hal_printf("APIC Base: 0x%X\n", apic_base);
//hal_printf("APIC Enabled: %s\n", apic_base_reg & bit_mask_64(11) ? "Yes" : "No");
//hal_printf("BSP: %s\n", apic_base_reg & bit_mask_64(8) ? "Yes" : "No");
//hal_printf("APIC Spour: 0x%X\n", *(uint32_t *) ((char *) apic_base + APIC_SPURIOUS_INT_VEC_REG_OFFSET));
//hal_printf("APIC Base: 0x%X\n", apic_base);
//hal_printf("APIC Enabled: %s\n", apic_base_reg & bit_mask_64(11) ? "Yes" : "No");
//hal_printf("BSP: %s\n", apic_base_reg & bit_mask_64(8) ? "Yes" : "No");
//hal_printf("APIC Spour: 0x%X\n", *(uint32_t *) ((char *) apic_base + APIC_SPURIOUS_INT_VEC_REG_OFFSET));
// hardware enable APIC
ecx = MSR_IA32_APIC_BASE;
eax = (uint32_t) ((apic_base_reg & lb_bit_field_mask(0, 31)) | lb_bit_mask(11));
hal_write_msr(&ecx, &edx, &eax);
// hardware enable APIC
ecx = MSR_IA32_APIC_BASE;
eax = (uint32_t) ((apic_base_reg & lb_bit_field_mask(0, 31)) | lb_bit_mask(11));
hal_write_msr(&ecx, &edx, &eax);
// software enable APIC
// hal_write_mem_32((char *) apic_base + APIC_SPURIOUS_INT_VEC_REG_OFFSET, *(uint32_t *) (apic_base + APIC_SPURIOUS_INT_VEC_REG_OFFSET) | (uint32_t)lb_bit_mask(8));
// software enable APIC
// hal_write_mem_32((char *) apic_base + APIC_SPURIOUS_INT_VEC_REG_OFFSET, *(uint32_t *) (apic_base + APIC_SPURIOUS_INT_VEC_REG_OFFSET) | (uint32_t)lb_bit_mask(8));
// hal_issue_interrupt(1, 255);
// hal_enable_interrupt();
return 0;
return 0;
}

200
hal/mem.c
View File

@ -21,7 +21,7 @@ char kernel_heap[KERNEL_HEAP_SIZE];
* @param pt_base page table base paddr
* @param pt_end page table entry paddr
*/
status_t KABI hal_write_initial_page_table(void* multiboot_info)
status_t KABI hal_write_initial_page_table(void *multiboot_info)
{
UNREFERENCED(multiboot_info);
@ -66,132 +66,142 @@ status_t KABI hal_write_initial_page_table(void* multiboot_info)
void KABI hal_write_pt(void *const base, uintptr_t const p_addr, uint64_t const attr)
{
if (base == NULL)
return;
uint64_t entry = (p_addr & 0xFFFFFFFFFF000) | attr;
((uint8_t *) base)[0] = (uint8_t) (entry & 0xFF);
((uint8_t *) base)[1] = (uint8_t) ((entry >> 8) & 0xFF);
((uint8_t *) base)[2] = (uint8_t) ((entry >> 16) & 0xFF);
((uint8_t *) base)[3] = (uint8_t) ((entry >> 24) & 0xFF);
((uint8_t *) base)[4] = (uint8_t) ((entry >> 32) & 0xFF);
((uint8_t *) base)[5] = (uint8_t) ((entry >> 40) & 0xFF);
((uint8_t *) base)[6] = (uint8_t) ((entry >> 48) & 0xFF);
((uint8_t *) base)[7] = (uint8_t) ((entry >> 56) & 0xFF);
return;
if (base == NULL)
{
return;
}
uint64_t entry = (p_addr & 0xFFFFFFFFFF000) | attr;
((uint8_t *) base)[0] = (uint8_t) (entry & 0xFF);
((uint8_t *) base)[1] = (uint8_t) ((entry >> 8) & 0xFF);
((uint8_t *) base)[2] = (uint8_t) ((entry >> 16) & 0xFF);
((uint8_t *) base)[3] = (uint8_t) ((entry >> 24) & 0xFF);
((uint8_t *) base)[4] = (uint8_t) ((entry >> 32) & 0xFF);
((uint8_t *) base)[5] = (uint8_t) ((entry >> 40) & 0xFF);
((uint8_t *) base)[6] = (uint8_t) ((entry >> 48) & 0xFF);
((uint8_t *) base)[7] = (uint8_t) ((entry >> 56) & 0xFF);
return;
}
void KABI hal_write_pd(void *const base, uintptr_t const pt_addr, uint64_t const attr)
{
if (base == NULL)
return;
uint64_t entry = (pt_addr & 0xFFFFFFFFFF000) | attr;
((uint8_t *) base)[0] = (uint8_t) (entry & 0xFF);
((uint8_t *) base)[1] = (uint8_t) ((entry >> 8) & 0xFF);
((uint8_t *) base)[2] = (uint8_t) ((entry >> 16) & 0xFF);
((uint8_t *) base)[3] = (uint8_t) ((entry >> 24) & 0xFF);
((uint8_t *) base)[4] = (uint8_t) ((entry >> 32) & 0xFF);
((uint8_t *) base)[5] = (uint8_t) ((entry >> 40) & 0xFF);
((uint8_t *) base)[6] = (uint8_t) ((entry >> 48) & 0xFF);
((uint8_t *) base)[7] = (uint8_t) ((entry >> 56) & 0xFF);
return;
if (base == NULL)
{
return;
}
uint64_t entry = (pt_addr & 0xFFFFFFFFFF000) | attr;
((uint8_t *) base)[0] = (uint8_t) (entry & 0xFF);
((uint8_t *) base)[1] = (uint8_t) ((entry >> 8) & 0xFF);
((uint8_t *) base)[2] = (uint8_t) ((entry >> 16) & 0xFF);
((uint8_t *) base)[3] = (uint8_t) ((entry >> 24) & 0xFF);
((uint8_t *) base)[4] = (uint8_t) ((entry >> 32) & 0xFF);
((uint8_t *) base)[5] = (uint8_t) ((entry >> 40) & 0xFF);
((uint8_t *) base)[6] = (uint8_t) ((entry >> 48) & 0xFF);
((uint8_t *) base)[7] = (uint8_t) ((entry >> 56) & 0xFF);
return;
}
void KABI hal_write_pdpt(void *const base, uintptr_t const pd_addr, uint64_t const attr)
{
if (base == NULL)
return;
uint64_t entry = (pd_addr & 0xFFFFFFFFFF000) | attr;
((uint8_t *) base)[0] = (uint8_t) (entry & 0xFF);
((uint8_t *) base)[1] = (uint8_t) ((entry >> 8) & 0xFF);
((uint8_t *) base)[2] = (uint8_t) ((entry >> 16) & 0xFF);
((uint8_t *) base)[3] = (uint8_t) ((entry >> 24) & 0xFF);
((uint8_t *) base)[4] = (uint8_t) ((entry >> 32) & 0xFF);
((uint8_t *) base)[5] = (uint8_t) ((entry >> 40) & 0xFF);
((uint8_t *) base)[6] = (uint8_t) ((entry >> 48) & 0xFF);
((uint8_t *) base)[7] = (uint8_t) ((entry >> 56) & 0xFF);
return;
if (base == NULL)
{
return;
}
uint64_t entry = (pd_addr & 0xFFFFFFFFFF000) | attr;
((uint8_t *) base)[0] = (uint8_t) (entry & 0xFF);
((uint8_t *) base)[1] = (uint8_t) ((entry >> 8) & 0xFF);
((uint8_t *) base)[2] = (uint8_t) ((entry >> 16) & 0xFF);
((uint8_t *) base)[3] = (uint8_t) ((entry >> 24) & 0xFF);
((uint8_t *) base)[4] = (uint8_t) ((entry >> 32) & 0xFF);
((uint8_t *) base)[5] = (uint8_t) ((entry >> 40) & 0xFF);
((uint8_t *) base)[6] = (uint8_t) ((entry >> 48) & 0xFF);
((uint8_t *) base)[7] = (uint8_t) ((entry >> 56) & 0xFF);
return;
}
void KABI hal_write_pml4(void *const base, uintptr_t const pdpt_addr, uint64_t const attr)
{
if (base == NULL)
return;
uint64_t const entry = (pdpt_addr & 0xFFFFFFFFFF000) | attr;
((uint8_t *) base)[0] = (uint8_t) (entry & 0xFF);
((uint8_t *) base)[1] = (uint8_t) ((entry >> 8) & 0xFF);
((uint8_t *) base)[2] = (uint8_t) ((entry >> 16) & 0xFF);
((uint8_t *) base)[3] = (uint8_t) ((entry >> 24) & 0xFF);
((uint8_t *) base)[4] = (uint8_t) ((entry >> 32) & 0xFF);
((uint8_t *) base)[5] = (uint8_t) ((entry >> 40) & 0xFF);
((uint8_t *) base)[6] = (uint8_t) ((entry >> 48) & 0xFF);
((uint8_t *) base)[7] = (uint8_t) ((entry >> 56) & 0xFF);
return;
if (base == NULL)
{
return;
}
uint64_t const entry = (pdpt_addr & 0xFFFFFFFFFF000) | attr;
((uint8_t *) base)[0] = (uint8_t) (entry & 0xFF);
((uint8_t *) base)[1] = (uint8_t) ((entry >> 8) & 0xFF);
((uint8_t *) base)[2] = (uint8_t) ((entry >> 16) & 0xFF);
((uint8_t *) base)[3] = (uint8_t) ((entry >> 24) & 0xFF);
((uint8_t *) base)[4] = (uint8_t) ((entry >> 32) & 0xFF);
((uint8_t *) base)[5] = (uint8_t) ((entry >> 40) & 0xFF);
((uint8_t *) base)[6] = (uint8_t) ((entry >> 48) & 0xFF);
((uint8_t *) base)[7] = (uint8_t) ((entry >> 56) & 0xFF);
return;
}
void KABI hal_write_segment_descriptor(void *const gdt, uint32_t const base, uint32_t const limit,
uint64_t const attr)
{
if (gdt == NULL)
return;
uint64_t const seg_desc = (((uint64_t) base & 0xFFFF) << 16) | ((((uint64_t) base >> 16) & 0xFF) << 32) |
((((uint64_t) base >> 24) & 0xFF) << 56) | ((uint64_t) limit & 0xFFFF) |
((((uint64_t) limit >> 16) & 0xF) << 48) | attr;
((uint8_t *) gdt)[0] = (uint8_t) (seg_desc & 0xFF);
((uint8_t *) gdt)[1] = (uint8_t) ((seg_desc >> 8) & 0xFF);
((uint8_t *) gdt)[2] = (uint8_t) ((seg_desc >> 16) & 0xFF);
((uint8_t *) gdt)[3] = (uint8_t) ((seg_desc >> 24) & 0xFF);
((uint8_t *) gdt)[4] = (uint8_t) ((seg_desc >> 32) & 0xFF);
((uint8_t *) gdt)[5] = (uint8_t) ((seg_desc >> 40) & 0xFF);
((uint8_t *) gdt)[6] = (uint8_t) ((seg_desc >> 48) & 0xFF);
((uint8_t *) gdt)[7] = (uint8_t) ((seg_desc >> 56) & 0xFF);
return;
if (gdt == NULL)
{
return;
}
uint64_t const seg_desc = (((uint64_t) base & 0xFFFF) << 16) | ((((uint64_t) base >> 16) & 0xFF) << 32) |
((((uint64_t) base >> 24) & 0xFF) << 56) | ((uint64_t) limit & 0xFFFF) |
((((uint64_t) limit >> 16) & 0xF) << 48) | attr;
((uint8_t *) gdt)[0] = (uint8_t) (seg_desc & 0xFF);
((uint8_t *) gdt)[1] = (uint8_t) ((seg_desc >> 8) & 0xFF);
((uint8_t *) gdt)[2] = (uint8_t) ((seg_desc >> 16) & 0xFF);
((uint8_t *) gdt)[3] = (uint8_t) ((seg_desc >> 24) & 0xFF);
((uint8_t *) gdt)[4] = (uint8_t) ((seg_desc >> 32) & 0xFF);
((uint8_t *) gdt)[5] = (uint8_t) ((seg_desc >> 40) & 0xFF);
((uint8_t *) gdt)[6] = (uint8_t) ((seg_desc >> 48) & 0xFF);
((uint8_t *) gdt)[7] = (uint8_t) ((seg_desc >> 56) & 0xFF);
return;
}
void *KABI halloc(uint32_t size)
{
return lb_salloc(kernel_heap, size);
return lb_salloc(kernel_heap, size);
}
void KABI hfree(void *ptr)
{
lb_sfree(kernel_heap, ptr);
return;
lb_sfree(kernel_heap, ptr);
return;
}
static void KABI _hal_init_gdt(void)
{
uint32_t coreid = hal_get_core_id();
// get gdt ready
hal_write_segment_descriptor((void *) &_gdts[coreid][0], 0, 0, 0);
hal_write_segment_descriptor((void *) &_gdts[coreid][8], 0, 0,
SEG_DPL_0 | SEG_CODE_DATA | SEG_PRESENT | SEG_LONG | SEG_TYPE_CODE_X);
hal_write_segment_descriptor((void *) &_gdts[coreid][16], 0, 0,
SEG_DPL_0 | SEG_CODE_DATA | SEG_PRESENT | SEG_LONG | SEG_TYPE_DATA_RW);
hal_write_segment_descriptor((void *) &_gdts[coreid][24], 0, 0,
SEG_DPL_3 | SEG_CODE_DATA | SEG_PRESENT | SEG_LONG | SEG_TYPE_CODE_X);
hal_write_segment_descriptor((void *) &_gdts[coreid][32], 0, 0,
SEG_DPL_3 | SEG_CODE_DATA | SEG_PRESENT | SEG_LONG | SEG_TYPE_DATA_RW);
uint32_t coreid = hal_get_core_id();
// get gdt ready
hal_write_segment_descriptor((void *) &_gdts[coreid][0], 0, 0, 0);
hal_write_segment_descriptor((void *) &_gdts[coreid][8], 0, 0,
SEG_DPL_0 | SEG_CODE_DATA | SEG_PRESENT | SEG_LONG | SEG_TYPE_CODE_X);
hal_write_segment_descriptor((void *) &_gdts[coreid][16], 0, 0,
SEG_DPL_0 | SEG_CODE_DATA | SEG_PRESENT | SEG_LONG | SEG_TYPE_DATA_RW);
hal_write_segment_descriptor((void *) &_gdts[coreid][24], 0, 0,
SEG_DPL_3 | SEG_CODE_DATA | SEG_PRESENT | SEG_LONG | SEG_TYPE_CODE_X);
hal_write_segment_descriptor((void *) &_gdts[coreid][32], 0, 0,
SEG_DPL_3 | SEG_CODE_DATA | SEG_PRESENT | SEG_LONG | SEG_TYPE_DATA_RW);
hal_write_segment_descriptor((void *) &_gdts[coreid][40], 0, 0xFFFFF,
SEG_DPL_0 | SEG_GRANULARITY | SEG_CODE_DATA | SEG_PRESENT | SEG_32_BITS |
SEG_TYPE_CODE_X);
hal_write_segment_descriptor((void *) &_gdts[coreid][48], 0, 0xFFFFF,
SEG_DPL_0 | SEG_GRANULARITY | SEG_CODE_DATA | SEG_PRESENT | SEG_32_BITS |
SEG_TYPE_DATA_RW);
hal_write_segment_descriptor((void *) &_gdts[coreid][56], 0, 0xFFFFF,
SEG_DPL_3 | SEG_GRANULARITY | SEG_CODE_DATA | SEG_PRESENT | SEG_32_BITS |
SEG_TYPE_CODE_X);
hal_write_segment_descriptor((void *) &_gdts[coreid][64], 0, 0xFFFFF,
SEG_DPL_3 | SEG_GRANULARITY | SEG_CODE_DATA | SEG_PRESENT | SEG_32_BITS |
SEG_TYPE_DATA_RW);
_gdt_ptrs[coreid].base = (uint64_t) &_gdts[coreid];
_gdt_ptrs[coreid].limit = GDT_ENTRY_NUM * GDT_ENTRY_SIZE - 1;
hal_flush_gdt(&_gdt_ptrs[coreid], seg_selector(1, 0), seg_selector(2, 0));
hal_write_segment_descriptor((void *) &_gdts[coreid][40], 0, 0xFFFFF,
SEG_DPL_0 | SEG_GRANULARITY | SEG_CODE_DATA | SEG_PRESENT | SEG_32_BITS |
SEG_TYPE_CODE_X);
hal_write_segment_descriptor((void *) &_gdts[coreid][48], 0, 0xFFFFF,
SEG_DPL_0 | SEG_GRANULARITY | SEG_CODE_DATA | SEG_PRESENT | SEG_32_BITS |
SEG_TYPE_DATA_RW);
hal_write_segment_descriptor((void *) &_gdts[coreid][56], 0, 0xFFFFF,
SEG_DPL_3 | SEG_GRANULARITY | SEG_CODE_DATA | SEG_PRESENT | SEG_32_BITS |
SEG_TYPE_CODE_X);
hal_write_segment_descriptor((void *) &_gdts[coreid][64], 0, 0xFFFFF,
SEG_DPL_3 | SEG_GRANULARITY | SEG_CODE_DATA | SEG_PRESENT | SEG_32_BITS |
SEG_TYPE_DATA_RW);
_gdt_ptrs[coreid].base = (uint64_t) &_gdts[coreid];
_gdt_ptrs[coreid].limit = GDT_ENTRY_NUM * GDT_ENTRY_SIZE - 1;
hal_flush_gdt(&_gdt_ptrs[coreid], seg_selector(1, 0), seg_selector(2, 0));
}
void KABI hal_mem_init()
{
_hal_init_gdt();
lb_salloc_init(kernel_heap, KERNEL_HEAP_SIZE);
return;
_hal_init_gdt();
lb_salloc_init(kernel_heap, KERNEL_HEAP_SIZE);
return;
}

View File

@ -5,203 +5,211 @@
#define get_column(pos) ((pos) % 80)
#define get_row(pos) ((pos) / 80)
#define get_pos(row,col) ((row) * 80 + (col))
#define get_pos(row, col) ((row) * 80 + (col))
static uint64_t text_pos;
void KABI hal_print_init(void)
{
text_pos = 0;
text_pos = 0;
}
static void KABI halp_print_scroll(void)
{
lb_mem_move((void *) (0xb8000 + get_pos(1, 0) * 2), (void *) (0xb8000 + get_pos(0, 0) * 2), (80 * 24) * 2);
return;
lb_mem_move((void *) (0xb8000 + get_pos(1, 0) * 2), (void *) (0xb8000 + get_pos(0, 0) * 2), (80 * 24) * 2);
return;
}
static void KABI halp_print_str(char const *str)
{
if(str == NULL)
return;
while (*str != 0)
{
if(*str == '\n')
{
text_pos = 80 * (get_row(text_pos) + 1);
if(text_pos > 80 * 25 - 1)
{
//can't hold
halp_print_scroll();
lb_mem_set((void *) (0xb8000 + 80 * 24 * 2), 0, 80 * 2); // clear last row
text_pos = 80 * 24;
}
str++;
}
else
{
if (text_pos > 80 * 25 - 1)
{
//can't hold
halp_print_scroll();
text_pos = 80 * 24;
}
*((char*)(0xb8000) + text_pos*2) = *str;
*((char*)(0xb8000) + text_pos*2 + 1) = 7;
str++;
text_pos++;
}
}
return;
if (str == NULL)
{
return;
}
while (*str != 0)
{
if (*str == '\n')
{
text_pos = 80 * (get_row(text_pos) + 1);
if (text_pos > 80 * 25 - 1)
{
//can't hold
halp_print_scroll();
lb_mem_set((void *) (0xb8000 + 80 * 24 * 2), 0, 80 * 2); // clear last row
text_pos = 80 * 24;
}
str++;
}
else
{
if (text_pos > 80 * 25 - 1)
{
//can't hold
halp_print_scroll();
text_pos = 80 * 24;
}
*((char *) (0xb8000) + text_pos * 2) = *str;
*((char *) (0xb8000) + text_pos * 2 + 1) = 7;
str++;
text_pos++;
}
}
return;
}
static void KABI halp_print_uint(uint64_t number)
{
char arr[21]; // do not need to initialize
arr[20] = 0; //zero-terminated
uint32_t index = 19;
uint32_t const div = 10;
while (1)
{
uint64_t quo = number / div;
uint64_t rmd = number % div;
number = quo;
arr[index--] = (char) ('0' + rmd);
if (number == 0)
break;
}
halp_print_str(&(arr[index + 1]));
return;
char arr[21]; // do not need to initialize
arr[20] = 0; //zero-terminated
uint32_t index = 19;
uint32_t const div = 10;
while (1)
{
uint64_t quo = number / div;
uint64_t rmd = number % div;
number = quo;
arr[index--] = (char) ('0' + rmd);
if (number == 0)
{
break;
}
}
halp_print_str(&(arr[index + 1]));
return;
}
static void KABI halp_print_int(int64_t number)
{
char arr[21]; // do not need to initialize
arr[20] = 0; //zero-terminated
uint32_t index = 19;
uint32_t isNegative = 0;
uint32_t const div = 10;
if (number < 0)
{
isNegative = 1;
number *= -1;
}
while (1)
{
int64_t quo = number / div;
int64_t rmd = number % div;
number = quo;
arr[index--] = (char) ('0' + rmd);
if (number == 0)
break;
}
if (isNegative)
{
arr[index--] = '-';
}
halp_print_str(&(arr[index + 1]));
return;
char arr[21]; // do not need to initialize
arr[20] = 0; //zero-terminated
uint32_t index = 19;
uint32_t isNegative = 0;
uint32_t const div = 10;
if (number < 0)
{
isNegative = 1;
number *= -1;
}
while (1)
{
int64_t quo = number / div;
int64_t rmd = number % div;
number = quo;
arr[index--] = (char) ('0' + rmd);
if (number == 0)
{
break;
}
}
if (isNegative)
{
arr[index--] = '-';
}
halp_print_str(&(arr[index + 1]));
return;
}
static void KABI halp_print_hex(uint64_t number, uint64_t capital)
{
char const lookup_table_cap[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char const lookup_table[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
char const * const look_up = capital == 1 ? &lookup_table_cap[0] : &lookup_table[0];
char arr[17];
arr[16] = 0; //zero-terminated
uint32_t index = 15;
uint32_t const div = 16;
while (1)
{
uint64_t quo = number / div;
uint64_t rmd = number % div;
number = quo;
arr[index--] = look_up[rmd];
if (number == 0)
break;
}
halp_print_str(&(arr[index + 1]));
return;
char const lookup_table_cap[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char const lookup_table[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
char const *const look_up = capital == 1 ? &lookup_table_cap[0] : &lookup_table[0];
char arr[17];
arr[16] = 0; //zero-terminated
uint32_t index = 15;
uint32_t const div = 16;
while (1)
{
uint64_t quo = number / div;
uint64_t rmd = number % div;
number = quo;
arr[index--] = look_up[rmd];
if (number == 0)
{
break;
}
}
halp_print_str(&(arr[index + 1]));
return;
}
void KABI hal_clear_screen(void)
{
text_pos = 0; // reset text_pos
lb_mem_set((void *) 0xb8000, 0, 25 * 80 * 2);
return;
text_pos = 0; // reset text_pos
lb_mem_set((void *) 0xb8000, 0, 25 * 80 * 2);
return;
}
void KABI hal_vprintf(char const * format, va_list args)
void KABI hal_vprintf(char const *format, va_list args)
{
char buf[2] = {0, 0};
int64_t d;
uint64_t u;
char* s;
char c;
for(;*format != '\0';format++)
{
if (*format != '%')
{
buf[0] = *format;
halp_print_str(buf);
continue;
}
format++;
switch (*format)
{
case 'd':
d = va_arg(args, int64_t);
halp_print_int(d);
break;
case 'u':
u = va_arg(args, uint64_t);
halp_print_uint(u);
break;
case 's':
s = va_arg(args, char *);
halp_print_str(s);
break;
case 'c':
c = (char)va_arg(args, int64_t);
buf[0] = c;
halp_print_str(buf);
break;
case 'x':
u = va_arg(args, uint64_t);
halp_print_hex(u, 0);
break;
case 'X':
u = va_arg(args, uint64_t);
halp_print_hex(u, 1);
break;
case '%':
buf[0] = '%';
halp_print_str(buf);
break;
default:
buf[0] = '%';
halp_print_str(buf);
format--;
break;
}
}
char buf[2] = {0, 0};
int64_t d;
uint64_t u;
char *s;
char c;
for (; *format != '\0'; format++)
{
if (*format != '%')
{
buf[0] = *format;
halp_print_str(buf);
continue;
}
format++;
switch (*format)
{
case 'd':
d = va_arg(args, int64_t);
halp_print_int(d);
break;
case 'u':
u = va_arg(args, uint64_t);
halp_print_uint(u);
break;
case 's':
s = va_arg(args, char *);
halp_print_str(s);
break;
case 'c':
c = (char) va_arg(args, int64_t);
buf[0] = c;
halp_print_str(buf);
break;
case 'x':
u = va_arg(args, uint64_t);
halp_print_hex(u, 0);
break;
case 'X':
u = va_arg(args, uint64_t);
halp_print_hex(u, 1);
break;
case '%':
buf[0] = '%';
halp_print_str(buf);
break;
default:
buf[0] = '%';
halp_print_str(buf);
format--;
break;
}
}
}
void KABI hal_printf(char const *format, ...)
{
va_list args;
va_start(args, format);
hal_vprintf(format, args);
va_end(args);
va_list args;
va_start(args, format);
hal_vprintf(format, args);
va_end(args);
}
void KABI hal_assert(uint32_t expression, char *message)
{
if (!expression)
{
hal_printf("HAL: Assertion failed. Detail: %s", message == NULL ? "NULL" : message);
hal_halt_cpu();
}
return;
if (!expression)
{
hal_printf("HAL: Assertion failed. Detail: %s", message == NULL ? "NULL" : message);
hal_halt_cpu();
}
return;
}

View File

@ -9,14 +9,14 @@
typedef struct
{
uint16_t limit;
uint64_t base;
uint16_t limit;
uint64_t base;
} STRUCT_PACKED hal_gdt_ptr_t;
typedef struct
{
uint16_t limit;
uint64_t base;
uint16_t limit;
uint64_t base;
} STRUCT_PACKED hal_idt_ptr_t;
@ -65,6 +65,7 @@ extern void KABI hal_read_idt(hal_idt_ptr_t **idt_ptr);
* Control Register Operations
*/
#define MSR_IA32_APIC_BASE 0x1B
extern void KABI hal_read_msr(uint32_t *ecx, uint32_t *edx, uint32_t *eax);
extern void KABI hal_write_msr(uint32_t *ecx, uint32_t *edx, uint32_t *eax);
@ -78,5 +79,4 @@ extern void KABI hal_write_cr8(uint64_t pri);
extern uint64_t KABI hal_read_cr8(void);
#endif

View File

@ -9,11 +9,11 @@
*/
typedef struct
{
const uint64_t rip;
const uint64_t cs;
const uint64_t rflags;
const uint64_t rsp;
const uint64_t ss;
const uint64_t rip;
const uint64_t cs;
const uint64_t rflags;
const uint64_t rsp;
const uint64_t ss;
} hal_interrupt_context_t;
@ -43,264 +43,518 @@ int32_t KABI hal_interrupt_init(void);
* System exception Handlers
*/
extern void KABI hal_interrupt_handler_0(void);
extern void KABI hal_interrupt_handler_1(void);
extern void KABI hal_interrupt_handler_2(void);
extern void KABI hal_interrupt_handler_3(void);
extern void KABI hal_interrupt_handler_4(void);
extern void KABI hal_interrupt_handler_5(void);
extern void KABI hal_interrupt_handler_6(void);
extern void KABI hal_interrupt_handler_7(void);
extern void KABI hal_interrupt_handler_8(void);
extern void KABI hal_interrupt_handler_9(void);
extern void KABI hal_interrupt_handler_10(void);
extern void KABI hal_interrupt_handler_11(void);
extern void KABI hal_interrupt_handler_12(void);
extern void KABI hal_interrupt_handler_13(void);
extern void KABI hal_interrupt_handler_14(void);
extern void KABI hal_interrupt_handler_15(void);
extern void KABI hal_interrupt_handler_16(void);
extern void KABI hal_interrupt_handler_17(void);
extern void KABI hal_interrupt_handler_18(void);
extern void KABI hal_interrupt_handler_19(void);
extern void KABI hal_interrupt_handler_20(void);
extern void KABI hal_interrupt_handler_21(void);
extern void KABI hal_interrupt_handler_22(void);
extern void KABI hal_interrupt_handler_23(void);
extern void KABI hal_interrupt_handler_24(void);
extern void KABI hal_interrupt_handler_25(void);
extern void KABI hal_interrupt_handler_26(void);
extern void KABI hal_interrupt_handler_27(void);
extern void KABI hal_interrupt_handler_28(void);
extern void KABI hal_interrupt_handler_29(void);
extern void KABI hal_interrupt_handler_30(void);
extern void KABI hal_interrupt_handler_31(void);
/**
* Kernel defined interrupt handlers
*/
extern void KABI hal_interrupt_handler_32(void);
extern void KABI hal_interrupt_handler_33(void);
extern void KABI hal_interrupt_handler_34(void);
extern void KABI hal_interrupt_handler_35(void);
extern void KABI hal_interrupt_handler_36(void);
extern void KABI hal_interrupt_handler_37(void);
extern void KABI hal_interrupt_handler_38(void);
extern void KABI hal_interrupt_handler_39(void);
extern void KABI hal_interrupt_handler_40(void);
extern void KABI hal_interrupt_handler_41(void);
extern void KABI hal_interrupt_handler_42(void);
extern void KABI hal_interrupt_handler_43(void);
extern void KABI hal_interrupt_handler_44(void);
extern void KABI hal_interrupt_handler_45(void);
extern void KABI hal_interrupt_handler_46(void);
extern void KABI hal_interrupt_handler_47(void);
extern void KABI hal_interrupt_handler_48(void);
extern void KABI hal_interrupt_handler_49(void);
extern void KABI hal_interrupt_handler_50(void);
extern void KABI hal_interrupt_handler_51(void);
extern void KABI hal_interrupt_handler_52(void);
extern void KABI hal_interrupt_handler_53(void);
extern void KABI hal_interrupt_handler_54(void);
extern void KABI hal_interrupt_handler_55(void);
extern void KABI hal_interrupt_handler_56(void);
extern void KABI hal_interrupt_handler_57(void);
extern void KABI hal_interrupt_handler_58(void);
extern void KABI hal_interrupt_handler_59(void);
extern void KABI hal_interrupt_handler_60(void);
extern void KABI hal_interrupt_handler_61(void);
extern void KABI hal_interrupt_handler_62(void);
extern void KABI hal_interrupt_handler_63(void);
extern void KABI hal_interrupt_handler_64(void);
extern void KABI hal_interrupt_handler_65(void);
extern void KABI hal_interrupt_handler_66(void);
extern void KABI hal_interrupt_handler_67(void);
extern void KABI hal_interrupt_handler_68(void);
extern void KABI hal_interrupt_handler_69(void);
extern void KABI hal_interrupt_handler_70(void);
extern void KABI hal_interrupt_handler_71(void);
extern void KABI hal_interrupt_handler_72(void);
extern void KABI hal_interrupt_handler_73(void);
extern void KABI hal_interrupt_handler_74(void);
extern void KABI hal_interrupt_handler_75(void);
extern void KABI hal_interrupt_handler_76(void);
extern void KABI hal_interrupt_handler_77(void);
extern void KABI hal_interrupt_handler_78(void);
extern void KABI hal_interrupt_handler_79(void);
extern void KABI hal_interrupt_handler_80(void);
extern void KABI hal_interrupt_handler_81(void);
extern void KABI hal_interrupt_handler_82(void);
extern void KABI hal_interrupt_handler_83(void);
extern void KABI hal_interrupt_handler_84(void);
extern void KABI hal_interrupt_handler_85(void);
extern void KABI hal_interrupt_handler_86(void);
extern void KABI hal_interrupt_handler_87(void);
extern void KABI hal_interrupt_handler_88(void);
extern void KABI hal_interrupt_handler_89(void);
extern void KABI hal_interrupt_handler_90(void);
extern void KABI hal_interrupt_handler_91(void);
extern void KABI hal_interrupt_handler_92(void);
extern void KABI hal_interrupt_handler_93(void);
extern void KABI hal_interrupt_handler_94(void);
extern void KABI hal_interrupt_handler_95(void);
extern void KABI hal_interrupt_handler_96(void);
extern void KABI hal_interrupt_handler_97(void);
extern void KABI hal_interrupt_handler_98(void);
extern void KABI hal_interrupt_handler_99(void);
extern void KABI hal_interrupt_handler_100(void);
extern void KABI hal_interrupt_handler_101(void);
extern void KABI hal_interrupt_handler_102(void);
extern void KABI hal_interrupt_handler_103(void);
extern void KABI hal_interrupt_handler_104(void);
extern void KABI hal_interrupt_handler_105(void);
extern void KABI hal_interrupt_handler_106(void);
extern void KABI hal_interrupt_handler_107(void);
extern void KABI hal_interrupt_handler_108(void);
extern void KABI hal_interrupt_handler_109(void);
extern void KABI hal_interrupt_handler_110(void);
extern void KABI hal_interrupt_handler_111(void);
extern void KABI hal_interrupt_handler_112(void);
extern void KABI hal_interrupt_handler_113(void);
extern void KABI hal_interrupt_handler_114(void);
extern void KABI hal_interrupt_handler_115(void);
extern void KABI hal_interrupt_handler_116(void);
extern void KABI hal_interrupt_handler_117(void);
extern void KABI hal_interrupt_handler_118(void);
extern void KABI hal_interrupt_handler_119(void);
extern void KABI hal_interrupt_handler_120(void);
extern void KABI hal_interrupt_handler_121(void);
extern void KABI hal_interrupt_handler_122(void);
extern void KABI hal_interrupt_handler_123(void);
extern void KABI hal_interrupt_handler_124(void);
extern void KABI hal_interrupt_handler_125(void);
extern void KABI hal_interrupt_handler_126(void);
extern void KABI hal_interrupt_handler_127(void);
extern void KABI hal_interrupt_handler_128(void);
extern void KABI hal_interrupt_handler_129(void);
extern void KABI hal_interrupt_handler_130(void);
extern void KABI hal_interrupt_handler_131(void);
extern void KABI hal_interrupt_handler_132(void);
extern void KABI hal_interrupt_handler_133(void);
extern void KABI hal_interrupt_handler_134(void);
extern void KABI hal_interrupt_handler_135(void);
extern void KABI hal_interrupt_handler_136(void);
extern void KABI hal_interrupt_handler_137(void);
extern void KABI hal_interrupt_handler_138(void);
extern void KABI hal_interrupt_handler_139(void);
extern void KABI hal_interrupt_handler_140(void);
extern void KABI hal_interrupt_handler_141(void);
extern void KABI hal_interrupt_handler_142(void);
extern void KABI hal_interrupt_handler_143(void);
extern void KABI hal_interrupt_handler_144(void);
extern void KABI hal_interrupt_handler_145(void);
extern void KABI hal_interrupt_handler_146(void);
extern void KABI hal_interrupt_handler_147(void);
extern void KABI hal_interrupt_handler_148(void);
extern void KABI hal_interrupt_handler_149(void);
extern void KABI hal_interrupt_handler_150(void);
extern void KABI hal_interrupt_handler_151(void);
extern void KABI hal_interrupt_handler_152(void);
extern void KABI hal_interrupt_handler_153(void);
extern void KABI hal_interrupt_handler_154(void);
extern void KABI hal_interrupt_handler_155(void);
extern void KABI hal_interrupt_handler_156(void);
extern void KABI hal_interrupt_handler_157(void);
extern void KABI hal_interrupt_handler_158(void);
extern void KABI hal_interrupt_handler_159(void);
extern void KABI hal_interrupt_handler_160(void);
extern void KABI hal_interrupt_handler_161(void);
extern void KABI hal_interrupt_handler_162(void);
extern void KABI hal_interrupt_handler_163(void);
extern void KABI hal_interrupt_handler_164(void);
extern void KABI hal_interrupt_handler_165(void);
extern void KABI hal_interrupt_handler_166(void);
extern void KABI hal_interrupt_handler_167(void);
extern void KABI hal_interrupt_handler_168(void);
extern void KABI hal_interrupt_handler_169(void);
extern void KABI hal_interrupt_handler_170(void);
extern void KABI hal_interrupt_handler_171(void);
extern void KABI hal_interrupt_handler_172(void);
extern void KABI hal_interrupt_handler_173(void);
extern void KABI hal_interrupt_handler_174(void);
extern void KABI hal_interrupt_handler_175(void);
extern void KABI hal_interrupt_handler_176(void);
extern void KABI hal_interrupt_handler_177(void);
extern void KABI hal_interrupt_handler_178(void);
extern void KABI hal_interrupt_handler_179(void);
extern void KABI hal_interrupt_handler_180(void);
extern void KABI hal_interrupt_handler_181(void);
extern void KABI hal_interrupt_handler_182(void);
extern void KABI hal_interrupt_handler_183(void);
extern void KABI hal_interrupt_handler_184(void);
extern void KABI hal_interrupt_handler_185(void);
extern void KABI hal_interrupt_handler_186(void);
extern void KABI hal_interrupt_handler_187(void);
extern void KABI hal_interrupt_handler_188(void);
extern void KABI hal_interrupt_handler_189(void);
extern void KABI hal_interrupt_handler_190(void);
extern void KABI hal_interrupt_handler_191(void);
extern void KABI hal_interrupt_handler_192(void);
extern void KABI hal_interrupt_handler_193(void);
extern void KABI hal_interrupt_handler_194(void);
extern void KABI hal_interrupt_handler_195(void);
extern void KABI hal_interrupt_handler_196(void);
extern void KABI hal_interrupt_handler_197(void);
extern void KABI hal_interrupt_handler_198(void);
extern void KABI hal_interrupt_handler_199(void);
extern void KABI hal_interrupt_handler_200(void);
extern void KABI hal_interrupt_handler_201(void);
extern void KABI hal_interrupt_handler_202(void);
extern void KABI hal_interrupt_handler_203(void);
extern void KABI hal_interrupt_handler_204(void);
extern void KABI hal_interrupt_handler_205(void);
extern void KABI hal_interrupt_handler_206(void);
extern void KABI hal_interrupt_handler_207(void);
extern void KABI hal_interrupt_handler_208(void);
extern void KABI hal_interrupt_handler_209(void);
extern void KABI hal_interrupt_handler_210(void);
extern void KABI hal_interrupt_handler_211(void);
extern void KABI hal_interrupt_handler_212(void);
extern void KABI hal_interrupt_handler_213(void);
extern void KABI hal_interrupt_handler_214(void);
extern void KABI hal_interrupt_handler_215(void);
extern void KABI hal_interrupt_handler_216(void);
extern void KABI hal_interrupt_handler_217(void);
extern void KABI hal_interrupt_handler_218(void);
extern void KABI hal_interrupt_handler_219(void);
extern void KABI hal_interrupt_handler_220(void);
extern void KABI hal_interrupt_handler_221(void);
extern void KABI hal_interrupt_handler_222(void);
extern void KABI hal_interrupt_handler_223(void);
extern void KABI hal_interrupt_handler_224(void);
extern void KABI hal_interrupt_handler_225(void);
extern void KABI hal_interrupt_handler_226(void);
extern void KABI hal_interrupt_handler_227(void);
extern void KABI hal_interrupt_handler_228(void);
extern void KABI hal_interrupt_handler_229(void);
extern void KABI hal_interrupt_handler_230(void);
extern void KABI hal_interrupt_handler_231(void);
extern void KABI hal_interrupt_handler_232(void);
extern void KABI hal_interrupt_handler_233(void);
extern void KABI hal_interrupt_handler_234(void);
extern void KABI hal_interrupt_handler_235(void);
extern void KABI hal_interrupt_handler_236(void);
extern void KABI hal_interrupt_handler_237(void);
extern void KABI hal_interrupt_handler_238(void);
extern void KABI hal_interrupt_handler_239(void);
extern void KABI hal_interrupt_handler_240(void);
extern void KABI hal_interrupt_handler_241(void);
extern void KABI hal_interrupt_handler_242(void);
extern void KABI hal_interrupt_handler_243(void);
extern void KABI hal_interrupt_handler_244(void);
extern void KABI hal_interrupt_handler_245(void);
extern void KABI hal_interrupt_handler_246(void);
extern void KABI hal_interrupt_handler_247(void);
extern void KABI hal_interrupt_handler_248(void);
extern void KABI hal_interrupt_handler_249(void);
extern void KABI hal_interrupt_handler_250(void);
extern void KABI hal_interrupt_handler_251(void);
extern void KABI hal_interrupt_handler_252(void);
extern void KABI hal_interrupt_handler_253(void);
extern void KABI hal_interrupt_handler_254(void);
extern void KABI hal_interrupt_handler_255(void);

View File

@ -75,7 +75,7 @@
static inline uint32_t KABI seg_selector(uint32_t index, uint32_t rpl)
{
return (index << 3) + rpl;
return (index << 3) + rpl;
}
void KABI hal_write_segment_descriptor(void *const gdt, uint32_t const base, uint32_t const limit, uint64_t const attr);

View File

@ -12,9 +12,9 @@
typedef struct
{
void* krnl_end;
intr_info_t intr_info;
char cpu_vd_str[13];
void *krnl_end;
intr_info_t intr_info;
char cpu_vd_str[13];
} boot_info_t;
status_t KABI hal_init(void *m_info);

View File

@ -13,7 +13,9 @@ typedef uint32_t irql_t;
#define IRQL_PASSIVE_LEVEL (1 << 0)
irql_t KABI hal_set_irql(irql_t irql);
irql_t KABI hal_get_irql(void);
uint32_t KABI hal_get_core_id(void);
/**
@ -21,14 +23,14 @@ uint32_t KABI hal_get_core_id(void);
*/
typedef struct
{
uint32_t timer_intr_vec;
uint32_t apc_intr_vec;
uint32_t dpc_intr_vec;
uint32_t timer_intr_vec;
uint32_t apc_intr_vec;
uint32_t dpc_intr_vec;
} intr_info_t;
typedef void (KABI * intr_handler_t)(void *context, void *intr_stack);
typedef void (KABI *intr_handler_t)(void *context, void *intr_stack);
void KABI hal_register_interrupt_handler(uint32_t coreid, uint32_t index, intr_handler_t handler, void* context);
void KABI hal_register_interrupt_handler(uint32_t coreid, uint32_t index, intr_handler_t handler, void *context);
void KABI hal_deregister_interrupt_handler(uint32_t coreid, uint32_t index);
@ -37,13 +39,13 @@ void KABI hal_deregister_interrupt_handler(uint32_t coreid, uint32_t index);
*/
typedef enum
{
unrecoverable_exc,
div_by_zero_exc,
general_protection_exc,
invalid_op_exc,
page_fault_exc,
unsupported_exc,
debug_exc
unrecoverable_exc,
div_by_zero_exc,
general_protection_exc,
invalid_op_exc,
page_fault_exc,
unsupported_exc,
debug_exc
} exc_type_t;
typedef void (KABI *exc_handler_t)(uint64_t exc_addr, uint64_t exc_stack, uint64_t error_code);

View File

@ -41,15 +41,15 @@
*/
typedef struct
{
uintptr_t base;
uint64_t size;
uint32_t attr;
uintptr_t base;
uint64_t size;
uint32_t attr;
} pmm_node_t;
typedef struct
{
uint32_t num_of_nodes;
pmm_node_t nodes[];
uint32_t num_of_nodes;
pmm_node_t nodes[];
} pmm_info_t;
#endif

View File

@ -3,6 +3,6 @@
#include "type.h"
void KABI hal_vprintf(const char* str, va_list args);
void KABI hal_vprintf(const char *str, va_list args);
#endif

View File

@ -5,7 +5,7 @@
void KABI ke_alloc_init(void);
void* KABI ke_alloc(uint32_t size);
void *KABI ke_alloc(uint32_t size);
void KABI ke_free(void *ptr);

View File

@ -3,7 +3,7 @@
#include "type.h"
void KABI ke_assert_ex(const char* expr_str, const char* file, int32_t line, int32_t expr);
void KABI ke_assert_ex(const char *expr_str, const char *file, int32_t line, int32_t expr);
#define ke_assert(expr) ke_assert_ex(#expr, __FILE__, __LINE__, expr)

View File

@ -4,7 +4,8 @@
#include "type.h"
#include "kernel/hal/print.h"
void KABI ke_printf(const char* str, ...);
void KABI ke_vprintf(const char* str, va_list args);
void KABI ke_printf(const char *str, ...);
void KABI ke_vprintf(const char *str, va_list args);
#endif

View File

@ -1,16 +1,17 @@
#ifndef _KERNEL_KE_RWLOCK_H_
#define _KERNEL_KE_RWLOCK_H_
#include "kernel/ke/spin_lock.h"
#include "type.h"
typedef struct
{
k_spin_lock_t w_mutex;
k_spin_lock_t r_mutex;
k_spin_lock_t res_lock;
k_spin_lock_t r_try;
uint32_t reader_ct;
uint32_t writer_ct;
k_spin_lock_t w_mutex;
k_spin_lock_t r_mutex;
k_spin_lock_t res_lock;
k_spin_lock_t r_try;
uint32_t reader_ct;
uint32_t writer_ct;
} k_rwwlock_t;
void KABI ke_rwwlock_init(k_rwwlock_t *lock);

View File

@ -6,7 +6,7 @@
typedef struct
{
int32_t val;
int32_t val;
} k_spin_lock_t;
void KABI ke_spin_lock_init(k_spin_lock_t *lock);

View File

@ -8,8 +8,8 @@ typedef uint32_t handle_t;
typedef struct
{
int32_t ref_count;
callback_func_t free_routine;
int32_t ref_count;
callback_func_t free_routine;
} ref_node_t;
#define K_HANDLE_BASE 0x80000000

View File

@ -6,11 +6,11 @@
typedef struct _k_avl_tree_node_t
{
struct _k_avl_tree_node_t *left;
struct _k_avl_tree_node_t *right;
struct _k_avl_tree_node_t *parent;
struct _k_avl_tree_node_t *left;
struct _k_avl_tree_node_t *right;
struct _k_avl_tree_node_t *parent;
int32_t height;
int32_t height;
} avl_tree_node_t;
/*
@ -22,8 +22,8 @@ typedef struct _k_avl_tree_node_t
*/
typedef struct _k_avl_tree_t
{
callback_func_t compare;
avl_tree_node_t *root;
callback_func_t compare;
avl_tree_node_t *root;
} avl_tree_t;
avl_tree_node_t *KABI lb_avl_tree_search(avl_tree_t *tree, avl_tree_node_t *entry);

View File

@ -5,14 +5,14 @@
typedef struct _linked_list_node_t
{
struct _linked_list_node_t *prev;
struct _linked_list_node_t *next;
struct _linked_list_node_t *prev;
struct _linked_list_node_t *next;
} linked_list_node_t;
typedef struct _linked_list_t
{
linked_list_node_t *head;
linked_list_node_t *tail;
linked_list_node_t *head;
linked_list_node_t *tail;
} linked_list_t;
/*

View File

@ -5,7 +5,7 @@
void KABI lb_salloc_init(void *base, uint32_t size);
void* KABI lb_salloc(void *base, uint32_t size);
void *KABI lb_salloc(void *base, uint32_t size);
void KABI lb_sfree(void *base, void *ptr);

View File

@ -21,39 +21,39 @@ void KABI lb_mem_set(void *src, uint8_t const val, uint64_t size);
static inline uint64_t KABI lb_align_down(uint64_t val, uint64_t alignment)
{
return (val / alignment) * alignment;
return (val / alignment) * alignment;
}
static inline uint64_t KABI lb_align_up(uint64_t val, uint64_t alignment)
{
return ((((val) % (alignment)) == 0) ? (((val) / (alignment)) * (alignment)) : (
(((val) / (alignment)) * (alignment)) + 1));
return ((((val) % (alignment)) == 0) ? (((val) / (alignment)) * (alignment)) : (
(((val) / (alignment)) * (alignment)) + 1));
}
static inline uint64_t KABI lb_is_overlap(uint64_t x1, uint64_t x2, uint64_t y1, uint64_t y2)
{
return ((x1 <= y2) && (y1 <= x2)) ? 1 : 0;
return ((x1 <= y2) && (y1 <= x2)) ? 1 : 0;
}
static inline int64_t KABI lb_max_64(int64_t a, int64_t b)
{
return (a) > (b) ? a : b;
return (a) > (b) ? a : b;
}
static inline int64_t KABI lb_min_64(int64_t a, int64_t b)
{
return (a) < (b) ? a : b;
return (a) < (b) ? a : b;
}
static inline int32_t KABI lb_max_32(int32_t a, int32_t b)
{
return (a) > (b) ? a : b;
return (a) > (b) ? a : b;
}
static inline int32_t KABI lb_min_32(int32_t a, int32_t b)
{
return (a) < (b) ? a : b;
return (a) < (b) ? a : b;
}
/*
@ -87,46 +87,46 @@ static inline uint32_t KAPI log_base_2(uint64_t num)
static inline uint64_t KABI lb_bit_mask(uint32_t bit)
{
return (uint64_t)1 << bit;
return (uint64_t) 1 << bit;
}
static inline uint64_t KABI lb_bit_field_mask(uint32_t low, uint32_t high)
{
return ~(~(uint64_t)0 << high << 1) << low;
return ~(~(uint64_t) 0 << high << 1) << low;
}
static inline void KABI lb_bit_map_set(void *bit_map, uint64_t bit)
{
if(bit_map != NULL)
{
uint64_t quot = bit >> 3;
uint32_t rmd = (uint32_t)(bit & lb_bit_field_mask(0, 2));
if (bit_map != NULL)
{
uint64_t quot = bit >> 3;
uint32_t rmd = (uint32_t) (bit & lb_bit_field_mask(0, 2));
*((uint8_t*)(bit_map) + quot) |= (uint8_t) lb_bit_mask(rmd);
}
*((uint8_t *) (bit_map) + quot) |= (uint8_t) lb_bit_mask(rmd);
}
}
static inline void KABI lb_bit_map_clear(void *bit_map, uint64_t bit)
{
if(bit_map != NULL)
{
uint64_t quot = bit >> 3;
uint32_t rmd = (uint32_t)(bit & lb_bit_field_mask(0, 2));
if (bit_map != NULL)
{
uint64_t quot = bit >> 3;
uint32_t rmd = (uint32_t) (bit & lb_bit_field_mask(0, 2));
*((uint8_t*)(bit_map) + quot) &= ~(uint8_t) lb_bit_mask(rmd);
}
*((uint8_t *) (bit_map) + quot) &= ~(uint8_t) lb_bit_mask(rmd);
}
}
static inline uint32_t KABI lb_bit_map_read(void *bit_map, uint64_t bit)
{
if(bit_map != NULL)
{
uint64_t quot = bit >> 3;
uint32_t rmd = (uint32_t)(bit & lb_bit_field_mask(0, 2));
if (bit_map != NULL)
{
uint64_t quot = bit >> 3;
uint32_t rmd = (uint32_t) (bit & lb_bit_field_mask(0, 2));
return (*((uint8_t*)(bit_map) + quot) & (uint8_t) lb_bit_mask(rmd)) == 0 ? 0 : 1;
}
return 0;
return (*((uint8_t *) (bit_map) + quot) & (uint8_t) lb_bit_mask(rmd)) == 0 ? 0 : 1;
}
return 0;
}
#endif

View File

@ -9,22 +9,22 @@ static uint8_t alloc_heap[K_KERNEL_HEAP_SIZE];
void KABI ke_alloc_init(void)
{
if (!alloc_initialized)
{
lb_salloc_init(alloc_heap, K_KERNEL_HEAP_SIZE);
alloc_initialized = true;
}
if (!alloc_initialized)
{
lb_salloc_init(alloc_heap, K_KERNEL_HEAP_SIZE);
alloc_initialized = true;
}
}
void *KABI ke_alloc(uint32_t size)
{
return alloc_initialized ? lb_salloc(alloc_heap, size) : NULL;
return alloc_initialized ? lb_salloc(alloc_heap, size) : NULL;
}
void KABI ke_free(void *ptr)
{
if (alloc_initialized)
{
lb_sfree(alloc_heap, ptr);
}
if (alloc_initialized)
{
lb_sfree(alloc_heap, ptr);
}
}

View File

@ -2,10 +2,10 @@
#include "kernel/ke/assert.h"
#include "kernel/ke/print.h"
void ke_assert_ex(const char* expr_str, const char* file, int32_t line, int32_t expr)
void ke_assert_ex(const char *expr_str, const char *file, int32_t line, int32_t expr)
{
if(!expr)
{
ke_printf("Assertion \"%s\" failed at %s:%d.\n", expr_str, file, line);
}
if (!expr)
{
ke_printf("Assertion \"%s\" failed at %s:%d.\n", expr_str, file, line);
}
}

View File

@ -3,15 +3,15 @@
int32_t KABI ke_interlocked_exchange_32(int32_t *target, int32_t val)
{
return hal_interlocked_exchange_32(target, val);
return hal_interlocked_exchange_32(target, val);
}
int32_t KABI ke_interlocked_increment_32(int32_t *target, int32_t increment)
{
return hal_interlocked_increment_32(target, increment);
return hal_interlocked_increment_32(target, increment);
}
int32_t KABI ke_interlocked_compare_exchange_32(int32_t *target, int32_t compare, int32_t val)
{
return hal_interlocked_compare_exchange_32(target, compare, val);
return hal_interlocked_compare_exchange_32(target, compare, val);
}

View File

@ -7,13 +7,13 @@
*/
void KABI ke_main(boot_info_t *boot_info)
{
status_t status = STATUS_SUCCESS;
status = hal_init(boot_info);
if (!sx_success(status))
{
ke_panic(status);
return;
}
status_t status = STATUS_SUCCESS;
status = hal_init(boot_info);
if (!sx_success(status))
{
ke_panic(status);
return;
}
ke_trap();
}

View File

@ -4,11 +4,12 @@
void KABI ke_trap(void)
{
while(true){};
while (true)
{};
}
void KABI ke_panic(uint64_t reason)
{
ke_printf("BugCheck: Reason - %ul\n", reason);
ke_trap();
ke_printf("BugCheck: Reason - %ul\n", reason);
ke_trap();
}

View File

@ -3,23 +3,23 @@
irql_t KABI ke_raise_irql(irql_t irql)
{
ke_assert(ke_get_irql() <= irql);
return hal_set_irql(irql);
ke_assert(ke_get_irql() <= irql);
return hal_set_irql(irql);
}
irql_t KABI ke_lower_irql(irql_t irql)
{
irql_t old_irql = ke_get_irql();
ke_assert(old_irql >= irql);
return hal_set_irql(irql);
irql_t old_irql = ke_get_irql();
ke_assert(old_irql >= irql);
return hal_set_irql(irql);
}
irql_t KABI ke_get_irql(void)
{
return hal_get_irql();
return hal_get_irql();
}
int KABI ke_get_current_core(void)
{
return hal_get_core_id();
return hal_get_core_id();
}

View File

@ -1,16 +1,16 @@
#include "kernel/ke/print.h"
void KABI ke_printf(const char* str, ...)
void KABI ke_printf(const char *str, ...)
{
va_list args;
va_start(args, str);
ke_vprintf(str, args);
va_end(args);
return;
va_list args;
va_start(args, str);
ke_vprintf(str, args);
va_end(args);
return;
}
void KABI ke_vprintf(const char* str, va_list args)
void KABI ke_vprintf(const char *str, va_list args)
{
hal_vprintf(str, args);
return;
hal_vprintf(str, args);
return;
}

View File

@ -3,98 +3,98 @@
void ke_rwwlock_init(k_rwwlock_t *lock)
{
if (lock != NULL)
{
ke_spin_lock_init(&lock->w_mutex);
ke_spin_lock_init(&lock->r_mutex);
ke_spin_lock_init(&lock->res_lock);
ke_spin_lock_init(&lock->r_try);
lock->reader_ct = 0;
lock->writer_ct = 0;
}
return;
if (lock != NULL)
{
ke_spin_lock_init(&lock->w_mutex);
ke_spin_lock_init(&lock->r_mutex);
ke_spin_lock_init(&lock->res_lock);
ke_spin_lock_init(&lock->r_try);
lock->reader_ct = 0;
lock->writer_ct = 0;
}
return;
}
void ke_rwwlock_reader_lock(k_rwwlock_t *lock)
{
if (lock != NULL)
{
ke_spin_lock(&lock->r_try);
ke_spin_lock(&lock->r_mutex);
lock->reader_ct++;
if (lock->reader_ct == 1)
{
ke_spin_lock(&lock->res_lock);
}
ke_spin_unlock(&lock->r_mutex);
ke_spin_unlock(&lock->r_try);
}
return;
if (lock != NULL)
{
ke_spin_lock(&lock->r_try);
ke_spin_lock(&lock->r_mutex);
lock->reader_ct++;
if (lock->reader_ct == 1)
{
ke_spin_lock(&lock->res_lock);
}
ke_spin_unlock(&lock->r_mutex);
ke_spin_unlock(&lock->r_try);
}
return;
}
void ke_rwwlock_reader_unlock(k_rwwlock_t *lock)
{
if (lock != NULL)
{
ke_spin_lock(&lock->r_mutex);
lock->reader_ct--;
if (lock->reader_ct == 0)
{
ke_spin_unlock(&lock->res_lock);
}
ke_spin_unlock(&lock->r_mutex);
}
return;
if (lock != NULL)
{
ke_spin_lock(&lock->r_mutex);
lock->reader_ct--;
if (lock->reader_ct == 0)
{
ke_spin_unlock(&lock->res_lock);
}
ke_spin_unlock(&lock->r_mutex);
}
return;
}
void ke_rwwlock_writer_lock(k_rwwlock_t *lock)
{
ke_spin_lock(&lock->w_mutex);
lock->writer_ct++;
if (lock->writer_ct == 1)
{
ke_spin_lock(&lock->r_try);
}
ke_spin_unlock(&lock->w_mutex);
ke_spin_lock(&lock->res_lock);
ke_spin_lock(&lock->w_mutex);
lock->writer_ct++;
if (lock->writer_ct == 1)
{
ke_spin_lock(&lock->r_try);
}
ke_spin_unlock(&lock->w_mutex);
ke_spin_lock(&lock->res_lock);
}
void ke_rwwlock_writer_unlock(k_rwwlock_t *lock)
{
ke_spin_unlock(&lock->res_lock);
ke_spin_lock(&lock->w_mutex);
lock->writer_ct--;
if (lock->writer_ct == 0)
{
ke_spin_unlock(&lock->r_try);
}
ke_spin_unlock(&lock->w_mutex);
ke_spin_unlock(&lock->res_lock);
ke_spin_lock(&lock->w_mutex);
lock->writer_ct--;
if (lock->writer_ct == 0)
{
ke_spin_unlock(&lock->r_try);
}
ke_spin_unlock(&lock->w_mutex);
}
irql_t ke_rwwlock_reader_lock_raise_irql(k_rwwlock_t *lock, irql_t irql)
{
irql_t old_irql = ke_raise_irql(irql);
ke_rwwlock_reader_lock(lock);
return old_irql;
irql_t old_irql = ke_raise_irql(irql);
ke_rwwlock_reader_lock(lock);
return old_irql;
}
void ke_rwwlock_reader_unlock_lower_irql(k_rwwlock_t *lock, irql_t irql)
{
ke_rwwlock_reader_unlock(lock);
ke_lower_irql(irql);
return;
ke_rwwlock_reader_unlock(lock);
ke_lower_irql(irql);
return;
}
irql_t ke_rwwlock_writer_lock_raise_irql(k_rwwlock_t *lock, irql_t irql)
{
irql_t old_irql = ke_raise_irql(irql);
ke_rwwlock_writer_lock(lock);
return old_irql;
irql_t old_irql = ke_raise_irql(irql);
ke_rwwlock_writer_lock(lock);
return old_irql;
}
void ke_rwwlock_writer_unlock_lower_irql(k_rwwlock_t *lock, irql_t irql)
{
ke_rwwlock_writer_unlock(lock);
ke_lower_irql(irql);
return;
ke_rwwlock_writer_unlock(lock);
ke_lower_irql(irql);
return;
}

View File

@ -3,47 +3,48 @@
void KABI ke_spin_lock_init(k_spin_lock_t *lock)
{
if (lock != NULL)
{
lock->val = 0;
}
if (lock != NULL)
{
lock->val = 0;
}
}
void KABI ke_spin_lock(k_spin_lock_t *lock)
{
if (lock != NULL)
{
while (ke_interlocked_compare_exchange_32(&lock->val, 0, 1) != 0);
}
return;
if (lock != NULL)
{
while (ke_interlocked_compare_exchange_32(&lock->val, 0, 1) != 0)
{}
}
return;
}
void KABI ke_spin_unlock(k_spin_lock_t *lock)
{
if (lock != NULL)
{
lock->val = 0;
}
return;
if (lock != NULL)
{
lock->val = 0;
}
return;
}
irql_t KABI ke_spin_lock_raise_irql(k_spin_lock_t *lock, irql_t irql)
{
irql_t prev_irql = ke_get_irql();
if (lock != NULL)
{
ke_raise_irql(irql);
ke_spin_lock(lock);
}
return prev_irql;
irql_t prev_irql = ke_get_irql();
if (lock != NULL)
{
ke_raise_irql(irql);
ke_spin_lock(lock);
}
return prev_irql;
}
void KABI ke_spin_unlock_lower_irql(k_spin_lock_t *lock, irql_t irql)
{
if (lock != NULL)
{
ke_spin_unlock(lock);
ke_lower_irql(irql);
}
return;
if (lock != NULL)
{
ke_spin_unlock(lock);
ke_lower_irql(irql);
}
return;
}

View File

@ -6,10 +6,10 @@
typedef struct
{
linked_list_node_t free_list_node;
avl_tree_node_t avl_tree_node;
uintptr_t base;
int32_t attr;
linked_list_node_t free_list_node;
avl_tree_node_t avl_tree_node;
uintptr_t base;
int32_t attr;
} physical_page_descriptor_t;
static avl_tree_t active_tree;
@ -26,61 +26,70 @@ static _Bool initialized;
*/
static int32_t mmp_base_paddr_compare(void *tree_node, void *my_node)
{
uintptr_t tree_base = OBTAIN_STRUCT_ADDR(tree_node,
physical_page_descriptor_t,
avl_tree_node)->base;
uintptr_t my_base = OBTAIN_STRUCT_ADDR(my_node,
physical_page_descriptor_t,
avl_tree_node)->base;
if (tree_base > my_base)
return 1;
else if (tree_base < my_base)
return -1;
else
return 0;
uintptr_t tree_base = OBTAIN_STRUCT_ADDR(tree_node,
physical_page_descriptor_t,
avl_tree_node)->base;
uintptr_t my_base = OBTAIN_STRUCT_ADDR(my_node,
physical_page_descriptor_t,
avl_tree_node)->base;
if (tree_base > my_base)
{
return 1;
}
else
{
if (tree_base < my_base)
{
return -1;
}
else
{
return 0;
}
}
}
status_t KABI sx_pmm_init(pmm_info_t *info)
{
if (info == NULL)
{
return MM_INVALID_ARGUMENTS;
}
if (info == NULL)
{
return MM_INVALID_ARGUMENTS;
}
if (initialized)
{
return STATUS_SUCCESS;
}
if (initialized)
{
return STATUS_SUCCESS;
}
ke_rwwlock_init(&lock);
lb_linked_list_init(&free_list);
lb_avl_tree_init(&active_tree, mmp_base_paddr_compare);
ke_rwwlock_init(&lock);
lb_linked_list_init(&free_list);
lb_avl_tree_init(&active_tree, mmp_base_paddr_compare);
for (uint32_t i = 0; i < info->num_of_nodes; i++)
{
pmm_node_t *each_node = &info->nodes[i];
for (uint32_t i = 0; i < info->num_of_nodes; i++)
{
pmm_node_t *each_node = &info->nodes[i];
ke_assert (each_node->base % KERNEL_PAGE_SIZE != 0);
ke_assert (each_node->base % KERNEL_PAGE_SIZE != 0);
for (uint64_t j = 0; j <= each_node->size; j++)
{
// note that k_alloc function here might trigger page fault
// however it's fine as long as we don't touch linked list just yet
// it will use the pages that are already on file to enlarge the kernel heap
// don't worry, be happy :)
physical_page_descriptor_t *page_info = ke_alloc(sizeof(physical_page_descriptor_t));
for (uint64_t j = 0; j <= each_node->size; j++)
{
// note that k_alloc function here might trigger page fault
// however it's fine as long as we don't touch linked list just yet
// it will use the pages that are already on file to enlarge the kernel heap
// don't worry, be happy :)
physical_page_descriptor_t *page_info = ke_alloc(sizeof(physical_page_descriptor_t));
if (page_info == NULL)
{
return MM_ALLOCATION_FAILED;
}
if (page_info == NULL)
{
return MM_ALLOCATION_FAILED;
}
page_info->base = each_node->base;
lb_linked_list_push_back(&free_list, &page_info->free_list_node);
}
}
initialized = true;
return STATUS_SUCCESS;
page_info->base = each_node->base;
lb_linked_list_push_back(&free_list, &page_info->free_list_node);
}
}
initialized = true;
return STATUS_SUCCESS;
}
// free lists can only be updated at IRQL == DISABLED
@ -90,99 +99,102 @@ status_t KABI sx_pmm_init(pmm_info_t *info)
status_t KABI mm_alloc_page(uintptr_t *out)
{
if (!initialized)
{
return MM_UNINITIALIZED;
}
if (!initialized)
{
return MM_UNINITIALIZED;
}
if (out == NULL)
{
return MM_INVALID_ARGUMENTS;
}
if (out == NULL)
{
return MM_INVALID_ARGUMENTS;
}
irql_t irql = ke_rwwlock_writer_lock_raise_irql(&lock, IRQL_DISABLED_LEVEL);
status_t result = STATUS_SUCCESS;
linked_list_node_t *node = NULL;
physical_page_descriptor_t *page_info = NULL;
node = lb_linked_list_pop_front(&free_list);
if (node != NULL)
{
page_info = OBTAIN_STRUCT_ADDR(node,
physical_page_descriptor_t,
free_list_node);
lb_avl_tree_insert(&active_tree, &page_info->avl_tree_node);
*out = page_info->base;
} else
{
result = MM_NOT_ENOUGH_PAGE;
}
irql_t irql = ke_rwwlock_writer_lock_raise_irql(&lock, IRQL_DISABLED_LEVEL);
status_t result = STATUS_SUCCESS;
linked_list_node_t *node = NULL;
physical_page_descriptor_t *page_info = NULL;
node = lb_linked_list_pop_front(&free_list);
if (node != NULL)
{
page_info = OBTAIN_STRUCT_ADDR(node,
physical_page_descriptor_t,
free_list_node);
lb_avl_tree_insert(&active_tree, &page_info->avl_tree_node);
*out = page_info->base;
}
else
{
result = MM_NOT_ENOUGH_PAGE;
}
ke_rwwlock_writer_unlock_lower_irql(&lock, irql);
return result;
ke_rwwlock_writer_unlock_lower_irql(&lock, irql);
return result;
}
status_t KABI mm_query_page_attr(uintptr_t base,
int32_t *out)
{
if (!initialized)
{
return MM_UNINITIALIZED;
}
if (!initialized)
{
return MM_UNINITIALIZED;
}
if (out == NULL)
{
return MM_INVALID_ARGUMENTS;
}
if (out == NULL)
{
return MM_INVALID_ARGUMENTS;
}
irql_t irql = ke_rwwlock_reader_lock_raise_irql(&lock, IRQL_DISABLED_LEVEL);
status_t result = STATUS_SUCCESS;
avl_tree_node_t *node = NULL;
// search for dummy
physical_page_descriptor_t dummy, *page_info = NULL;
dummy.base = base;
irql_t irql = ke_rwwlock_reader_lock_raise_irql(&lock, IRQL_DISABLED_LEVEL);
status_t result = STATUS_SUCCESS;
avl_tree_node_t *node = NULL;
// search for dummy
physical_page_descriptor_t dummy, *page_info = NULL;
dummy.base = base;
node = lb_avl_tree_delete(&active_tree, &dummy.avl_tree_node);
node = lb_avl_tree_delete(&active_tree, &dummy.avl_tree_node);
if (node != NULL)
{
page_info = OBTAIN_STRUCT_ADDR(node, physical_page_descriptor_t, avl_tree_node);
*out = page_info->attr;
} else
{
result = MM_INVALID_ARGUMENTS;
}
if (node != NULL)
{
page_info = OBTAIN_STRUCT_ADDR(node, physical_page_descriptor_t, avl_tree_node);
*out = page_info->attr;
}
else
{
result = MM_INVALID_ARGUMENTS;
}
ke_rwwlock_reader_unlock_lower_irql(&lock, irql);
ke_rwwlock_reader_unlock_lower_irql(&lock, irql);
return result;
return result;
}
status_t KABI mm_free_page(uintptr_t base)
{
if (!initialized)
{
return MM_UNINITIALIZED;
}
if (!initialized)
{
return MM_UNINITIALIZED;
}
// just lock since not sharing with anyone
irql_t irql = ke_rwwlock_writer_lock_raise_irql(&lock, IRQL_DISABLED_LEVEL);
status_t result = STATUS_SUCCESS;
avl_tree_node_t *node = NULL;
// search for dummy
physical_page_descriptor_t dummy, *page_info;
dummy.base = base;
// just lock since not sharing with anyone
irql_t irql = ke_rwwlock_writer_lock_raise_irql(&lock, IRQL_DISABLED_LEVEL);
status_t result = STATUS_SUCCESS;
avl_tree_node_t *node = NULL;
// search for dummy
physical_page_descriptor_t dummy, *page_info;
dummy.base = base;
node = lb_avl_tree_delete(&active_tree, &dummy.avl_tree_node);
if (node != NULL)
{
page_info = OBTAIN_STRUCT_ADDR(node, physical_page_descriptor_t, avl_tree_node);
lb_linked_list_push_back(&free_list, &page_info->free_list_node);
} else
{
result = MM_INVALID_ARGUMENTS;
}
node = lb_avl_tree_delete(&active_tree, &dummy.avl_tree_node);
if (node != NULL)
{
page_info = OBTAIN_STRUCT_ADDR(node, physical_page_descriptor_t, avl_tree_node);
lb_linked_list_push_back(&free_list, &page_info->free_list_node);
}
else
{
result = MM_INVALID_ARGUMENTS;
}
ke_rwwlock_writer_unlock_lower_irql(&lock, irql);
ke_rwwlock_writer_unlock_lower_irql(&lock, irql);
return result;
return result;
}

View File

@ -8,17 +8,17 @@
typedef struct
{
avl_tree_node_t tree_node;
handle_t handle;
ref_node_t *ref;
callback_func_t free_routine;
avl_tree_node_t tree_node;
handle_t handle;
ref_node_t *ref;
callback_func_t free_routine;
} handle_node_t;
static int32_t rfp_handle_node_free(void *node, void *up)
{
UNREFERENCED(up);
ke_free(node);
return 0;
UNREFERENCED(up);
ke_free(node);
return 0;
}
// ===========================
@ -32,215 +32,240 @@ static int32_t handle_base;
static int32_t rfp_handle_compare(void *tree_node, void *my_node)
{
handle_node_t *tcb = OBTAIN_STRUCT_ADDR(tree_node, handle_node_t, tree_node);
handle_node_t *my_tcb = OBTAIN_STRUCT_ADDR(my_node, handle_node_t, tree_node);
handle_node_t *tcb = OBTAIN_STRUCT_ADDR(tree_node, handle_node_t, tree_node);
handle_node_t *my_tcb = OBTAIN_STRUCT_ADDR(my_node, handle_node_t, tree_node);
if ((uintptr_t) tcb->handle > (uintptr_t) my_tcb->handle)
return -1;
else if ((uintptr_t) tcb->handle == (uintptr_t) my_tcb->handle)
return 0;
else
return 1;
if ((uintptr_t) tcb->handle > (uintptr_t) my_tcb->handle)
{
return -1;
}
else
{
if ((uintptr_t) tcb->handle == (uintptr_t) my_tcb->handle)
{
return 0;
}
else
{
return 1;
}
}
}
static handle_node_t *rfp_search_handle_node(handle_t handle)
{
avl_tree_node_t *result;
handle_node_t temp;
temp.handle = handle;
result = lb_avl_tree_search(&handle_tree, &temp.tree_node);
return result == NULL ? NULL : OBTAIN_STRUCT_ADDR(result, handle_node_t, tree_node);
avl_tree_node_t *result;
handle_node_t temp;
temp.handle = handle;
result = lb_avl_tree_search(&handle_tree, &temp.tree_node);
return result == NULL ? NULL : OBTAIN_STRUCT_ADDR(result, handle_node_t, tree_node);
}
status_t KABI rf_reference_setup(void)
{
if (!initialized)
{
lb_avl_tree_init(&handle_tree, rfp_handle_compare);
ke_spin_lock_init(&handle_tree_lock);
handle_base = K_HANDLE_BASE;
initialized = true;
}
return STATUS_SUCCESS;
if (!initialized)
{
lb_avl_tree_init(&handle_tree, rfp_handle_compare);
ke_spin_lock_init(&handle_tree_lock);
handle_base = K_HANDLE_BASE;
initialized = true;
}
return STATUS_SUCCESS;
}
status_t KABI rf_reference_create(ref_node_t *ref,
callback_func_t free_func)
{
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
if (ref == NULL || free_func == NULL)
return RF_INVALID_ARGUMENTS;
if (ref == NULL || free_func == NULL)
{
return RF_INVALID_ARGUMENTS;
}
ref->free_routine = free_func;
ref->ref_count = 1;
ref->free_routine = free_func;
ref->ref_count = 1;
return STATUS_SUCCESS;
return STATUS_SUCCESS;
}
status_t KABI rf_reference_obj(ref_node_t *ref_node)
{
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
if (ref_node == NULL)
return RF_INVALID_ARGUMENTS;
if (ref_node == NULL)
{
return RF_INVALID_ARGUMENTS;
}
int32_t old_ref_count = ke_interlocked_increment_32(&ref_node->ref_count, 1);
int32_t old_ref_count = ke_interlocked_increment_32(&ref_node->ref_count, 1);
ke_assert(old_ref_count >= 1);
ke_assert(old_ref_count >= 1);
return STATUS_SUCCESS;
return STATUS_SUCCESS;
}
status_t KABI rf_dereference_obj(ref_node_t *ref_node)
{
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
if (ref_node == NULL)
return RF_INVALID_ARGUMENTS;
if (ref_node == NULL)
{
return RF_INVALID_ARGUMENTS;
}
status_t result = STATUS_SUCCESS;
status_t result = STATUS_SUCCESS;
int32_t old_ref_count = ke_interlocked_increment_32(&ref_node->ref_count, -1);
int32_t old_ref_count = ke_interlocked_increment_32(&ref_node->ref_count, -1);
ke_assert(old_ref_count >= 1);
ke_assert(old_ref_count >= 1);
if (old_ref_count == 1)
{
ref_node->free_routine(ref_node, NULL);
}
if (old_ref_count == 1)
{
ref_node->free_routine(ref_node, NULL);
}
return result;
return result;
}
static status_t KABI rf_open_obj_by_handle(handle_t handle, ref_node_t **out)
{
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
if (!initialized)
{
return RF_UNINITIALIZED;
}
if (!initialized)
{
return RF_UNINITIALIZED;
}
if (out == NULL)
{
return RF_INVALID_ARGUMENTS;
}
if (out == NULL)
{
return RF_INVALID_ARGUMENTS;
}
irql_t irql;
status_t status = STATUS_SUCCESS;
ref_node_t *ref = NULL;
irql_t irql;
status_t status = STATUS_SUCCESS;
ref_node_t *ref = NULL;
irql = ke_spin_lock_raise_irql(&handle_tree_lock, IRQL_DPC_LEVEL);
handle_node_t *handle_node = rfp_search_handle_node(handle);
if (handle_node == NULL)
{
status = RF_INVALID_HANDLE;
} else
{
ref = handle_node->ref;
}
irql = ke_spin_lock_raise_irql(&handle_tree_lock, IRQL_DPC_LEVEL);
handle_node_t *handle_node = rfp_search_handle_node(handle);
if (handle_node == NULL)
{
status = RF_INVALID_HANDLE;
}
else
{
ref = handle_node->ref;
}
// PREREQUISITE: Having a handle -> having a reference
// MUST GUARANTEE that handle exists while we reference
if (sx_success(status))
{
// reference the object then return the reference
rf_reference_obj(ref);
*out = ref;
}
// PREREQUISITE: Having a handle -> having a reference
// MUST GUARANTEE that handle exists while we reference
if (sx_success(status))
{
// reference the object then return the reference
rf_reference_obj(ref);
*out = ref;
}
ke_spin_unlock_lower_irql(&handle_tree_lock, irql);
ke_spin_unlock_lower_irql(&handle_tree_lock, irql);
return status;
return status;
}
static status_t KABI rf_create_handle(ref_node_t *ref,
handle_node_t *node,
handle_t *out)
{
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
if (!initialized)
return RF_UNINITIALIZED;
if (!initialized)
{
return RF_UNINITIALIZED;
}
if (ref == NULL || node == NULL || out == NULL)
return RF_INVALID_ARGUMENTS;
if (ref == NULL || node == NULL || out == NULL)
{
return RF_INVALID_ARGUMENTS;
}
status_t result = STATUS_SUCCESS;
irql_t irql;
status_t result = STATUS_SUCCESS;
irql_t irql;
if (sx_success(result))
{
// TODO: CHECK OVERFLOW
node->handle = (handle_t) ke_interlocked_increment_32(&handle_base, 1);
node->ref = ref;
irql = ke_spin_lock_raise_irql(&handle_tree_lock, IRQL_DPC_LEVEL);
handle_node_t *existing_node = rfp_search_handle_node(node->handle);
if (existing_node == NULL)
{
lb_avl_tree_insert(&handle_tree, &node->tree_node);
} else
{
result = RF_DUPLICATED_HANDLE;
}
if (sx_success(result))
{
// TODO: CHECK OVERFLOW
node->handle = (handle_t) ke_interlocked_increment_32(&handle_base, 1);
node->ref = ref;
irql = ke_spin_lock_raise_irql(&handle_tree_lock, IRQL_DPC_LEVEL);
handle_node_t *existing_node = rfp_search_handle_node(node->handle);
if (existing_node == NULL)
{
lb_avl_tree_insert(&handle_tree, &node->tree_node);
}
else
{
result = RF_DUPLICATED_HANDLE;
}
ke_spin_unlock_lower_irql(&handle_tree_lock, irql);
}
ke_spin_unlock_lower_irql(&handle_tree_lock, irql);
}
if (sx_success(result))
{
rf_reference_obj(ref);
*out = node->handle;
} else
{
node->free_routine(node, NULL);
}
if (sx_success(result))
{
rf_reference_obj(ref);
*out = node->handle;
}
else
{
node->free_routine(node, NULL);
}
return result;
return result;
}
static status_t KABI rf_close_handle(handle_t handle)
{
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
if (!initialized)
return RF_UNINITIALIZED;
if (!initialized)
{
return RF_UNINITIALIZED;
}
irql_t irql;
status_t status = STATUS_SUCCESS;
ref_node_t *ref = NULL;
bool free = false;
irql_t irql;
status_t status = STATUS_SUCCESS;
ref_node_t *ref = NULL;
bool free = false;
irql = ke_spin_lock_raise_irql(&handle_tree_lock, IRQL_DPC_LEVEL);
handle_node_t *handle_node = rfp_search_handle_node(handle);
if (handle_node == NULL)
{
status = RF_INVALID_HANDLE;
} else
{
ref = handle_node->ref;
lb_avl_tree_delete(&handle_tree, &handle_node->tree_node);
free = true;
}
ke_spin_unlock_lower_irql(&handle_tree_lock, irql);
irql = ke_spin_lock_raise_irql(&handle_tree_lock, IRQL_DPC_LEVEL);
handle_node_t *handle_node = rfp_search_handle_node(handle);
if (handle_node == NULL)
{
status = RF_INVALID_HANDLE;
}
else
{
ref = handle_node->ref;
lb_avl_tree_delete(&handle_tree, &handle_node->tree_node);
free = true;
}
ke_spin_unlock_lower_irql(&handle_tree_lock, irql);
if (free)
{
handle_node->free_routine(handle_node, NULL);
}
if (free)
{
handle_node->free_routine(handle_node, NULL);
}
if (sx_success(status))
{
// dereference the object
rf_dereference_obj(ref);
}
if (sx_success(status))
{
// dereference the object
rf_dereference_obj(ref);
}
return status;
return status;
}
@ -250,46 +275,54 @@ static status_t KABI rf_close_handle(handle_t handle)
status_t KABI sx_create_handle(ref_node_t *ref, handle_t *out)
{
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
if (!initialized)
return RF_UNINITIALIZED;
if (!initialized)
{
return RF_UNINITIALIZED;
}
handle_node_t *node;
node = (handle_node_t *) ke_alloc(sizeof(handle_node_t));
if (node == NULL)
{
return RF_ALLOCATION_FAILED;
}
handle_node_t *node;
node = (handle_node_t *) ke_alloc(sizeof(handle_node_t));
if (node == NULL)
{
return RF_ALLOCATION_FAILED;
}
node->free_routine = rfp_handle_node_free;
node->free_routine = rfp_handle_node_free;
return rf_create_handle(ref, node, out);
return rf_create_handle(ref, node, out);
}
status_t KABI sx_close_handle(handle_t handle)
{
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
if (!initialized)
return RF_UNINITIALIZED;
if (!initialized)
{
return RF_UNINITIALIZED;
}
// need to keep sx version since need to do handle check here
// need to keep sx version since need to do handle check here
return rf_close_handle(handle);
return rf_close_handle(handle);
}
status_t KABI sx_open_obj_by_handle(handle_t handle, ref_node_t **out)
{
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
if (!initialized)
return RF_UNINITIALIZED;
if (!initialized)
{
return RF_UNINITIALIZED;
}
if (out == NULL)
return RF_INVALID_ARGUMENTS;
if (out == NULL)
{
return RF_INVALID_ARGUMENTS;
}
// check special handles first
// check special handles first
// if (handle == K_HANDLE_CURRENT_THREAD)
// {
// // no need to ref first since definitely current thread context
@ -299,5 +332,5 @@ status_t KABI sx_open_obj_by_handle(handle_t handle, ref_node_t **out)
// return STATUS_SUCCESS;
// }
return rf_open_obj_by_handle(handle, out);
return rf_open_obj_by_handle(handle, out);
}

View File

@ -2,89 +2,102 @@
static inline int32_t KABI lbp_avl_tree_node_get_height(avl_tree_node_t *node)
{
return node == NULL ? -1 : node->height;
return node == NULL ? -1 : node->height;
}
static inline int32_t KABI lbp_avl_tree_node_get_balance_factor(avl_tree_node_t *node)
{
if (node == NULL)
return 0;
return lbp_avl_tree_node_get_height(node->left) - lbp_avl_tree_node_get_height(node->right);
if (node == NULL)
{
return 0;
}
return lbp_avl_tree_node_get_height(node->left) - lbp_avl_tree_node_get_height(node->right);
}
static avl_tree_node_t *KABI lbp_avl_tree_node_right_rotate(avl_tree_node_t *root)
{
avl_tree_node_t *left_children = root->left;
//adjust parents first
left_children->parent = root->parent;
root->parent = left_children;
if (left_children->right != NULL)
left_children->right->parent = root;
//perform rotation
root->left = root->left->right;
left_children->right = root;
//adjust height
root->height = lb_max_32(lbp_avl_tree_node_get_height(root->left), lbp_avl_tree_node_get_height(root->right)) + 1;
left_children->height =
lb_max_32(lbp_avl_tree_node_get_height(left_children->left),
lbp_avl_tree_node_get_height(left_children->right)) + 1;
return left_children;
avl_tree_node_t *left_children = root->left;
//adjust parents first
left_children->parent = root->parent;
root->parent = left_children;
if (left_children->right != NULL)
{
left_children->right->parent = root;
}
//perform rotation
root->left = root->left->right;
left_children->right = root;
//adjust height
root->height = lb_max_32(lbp_avl_tree_node_get_height(root->left), lbp_avl_tree_node_get_height(root->right)) + 1;
left_children->height =
lb_max_32(lbp_avl_tree_node_get_height(left_children->left),
lbp_avl_tree_node_get_height(left_children->right)) + 1;
return left_children;
}
static avl_tree_node_t *KABI lbp_avl_tree_node_left_rotate(avl_tree_node_t *root)
{
avl_tree_node_t *right_children = root->right;
//adjust parents
right_children->parent = root->parent;
root->parent = right_children;
if (right_children->left != NULL)
right_children->left->parent = root;
//perform rotation
root->right = root->right->left;
right_children->left = root;
avl_tree_node_t *right_children = root->right;
//adjust parents
right_children->parent = root->parent;
root->parent = right_children;
if (right_children->left != NULL)
{
right_children->left->parent = root;
}
//perform rotation
root->right = root->right->left;
right_children->left = root;
root->height = lb_max_32(lbp_avl_tree_node_get_height(root->left), lbp_avl_tree_node_get_height(root->right)) + 1;
right_children->height =
lb_max_32(lbp_avl_tree_node_get_height(right_children->left),
lbp_avl_tree_node_get_height(right_children->right)) +
1;
return right_children;
root->height = lb_max_32(lbp_avl_tree_node_get_height(root->left), lbp_avl_tree_node_get_height(root->right)) + 1;
right_children->height =
lb_max_32(lbp_avl_tree_node_get_height(right_children->left),
lbp_avl_tree_node_get_height(right_children->right)) +
1;
return right_children;
}
static avl_tree_node_t *KABI lbp_avl_tree_node_balance(avl_tree_node_t *node)
{
const int32_t bf = lbp_avl_tree_node_get_balance_factor(node);
const int32_t bf = lbp_avl_tree_node_get_balance_factor(node);
if (bf > 1)
{
const int32_t left_bf = lbp_avl_tree_node_get_balance_factor(node->left);
if (left_bf >= 0)
//left left
return lbp_avl_tree_node_right_rotate(node);
else
{
//left right
node->left = lbp_avl_tree_node_left_rotate(node->left);
return lbp_avl_tree_node_right_rotate(node);
}
}
else if (bf < -1)
{
const int32_t right_bf = lbp_avl_tree_node_get_balance_factor(node->right);
if (right_bf <= 0)
{
// right right
return lbp_avl_tree_node_left_rotate(node);
}
else
{
// right left
node->right = lbp_avl_tree_node_right_rotate(node->right);
return lbp_avl_tree_node_left_rotate(node);
}
}
else
return node;
if (bf > 1)
{
const int32_t left_bf = lbp_avl_tree_node_get_balance_factor(node->left);
if (left_bf >= 0)
{
//left left
return lbp_avl_tree_node_right_rotate(node);
}
else
{
//left right
node->left = lbp_avl_tree_node_left_rotate(node->left);
return lbp_avl_tree_node_right_rotate(node);
}
}
else
{
if (bf < -1)
{
const int32_t right_bf = lbp_avl_tree_node_get_balance_factor(node->right);
if (right_bf <= 0)
{
// right right
return lbp_avl_tree_node_left_rotate(node);
}
else
{
// right left
node->right = lbp_avl_tree_node_right_rotate(node->right);
return lbp_avl_tree_node_left_rotate(node);
}
}
else
{
return node;
}
}
}
@ -92,147 +105,187 @@ static avl_tree_node_t *KABI lbp_avl_tree_node_insert(avl_tree_node_t *root, avl
callback_func_t compare,
avl_tree_node_t *parent)
{
if (node == NULL || compare == NULL)
return root;
if (root == NULL)
{
node->parent = parent;
return node;
}
if (node == NULL || compare == NULL)
{
return root;
}
if (root == NULL)
{
node->parent = parent;
return node;
}
const int32_t comp = compare(root, node);
if (comp < 0)
root->right = lbp_avl_tree_node_insert(root->right, node, compare, root);
else if (comp == 0)
return root;
else
root->left = lbp_avl_tree_node_insert(root->left, node, compare, root);
const int32_t comp = compare(root, node);
if (comp < 0)
{
root->right = lbp_avl_tree_node_insert(root->right, node, compare, root);
}
else
{
if (comp == 0)
{
return root;
}
else
{
root->left = lbp_avl_tree_node_insert(root->left, node, compare, root);
}
}
root->height = lb_max_32(lbp_avl_tree_node_get_height(root->left), lbp_avl_tree_node_get_height(root->right)) + 1;
root->height = lb_max_32(lbp_avl_tree_node_get_height(root->left), lbp_avl_tree_node_get_height(root->right)) + 1;
return lbp_avl_tree_node_balance(root);
return lbp_avl_tree_node_balance(root);
}
static void lbp_avl_tree_swap_nodes(avl_tree_node_t *node1, avl_tree_node_t *node2)
{
if (node1 == NULL || node2 == NULL)
return;
avl_tree_node_t *parent = NULL;
avl_tree_node_t *child = NULL;
avl_tree_node_t *temp = NULL;
//swap node but does not change anything else other than node1,node2
if (node1->parent != NULL && node1->parent == node2)
{
parent = node2;
child = node1;
}
else if (node2->parent != NULL && node2->parent == node1)
{
parent = node1;
child = node2;
}
if (node1 == NULL || node2 == NULL)
{
return;
}
avl_tree_node_t *parent = NULL;
avl_tree_node_t *child = NULL;
avl_tree_node_t *temp = NULL;
//swap node but does not change anything else other than node1,node2
if (node1->parent != NULL && node1->parent == node2)
{
parent = node2;
child = node1;
}
else
{
if (node2->parent != NULL && node2->parent == node1)
{
parent = node1;
child = node2;
}
}
if (parent != NULL && child != NULL)
{
//connected case
if (parent->parent != NULL)
{
if (parent->parent->left == parent)
parent->parent->left = child;
else
parent->parent->right = child;
}
//update left/right for parent
if (parent->left != NULL && child != parent->left)
{
parent->left->parent = child;
}
if (parent->right != NULL && child != parent->right)
{
parent->right->parent = child;
}
//update left/right for children
if (child->left != NULL)
child->left->parent = parent;
if (child->right != NULL)
child->right->parent = parent;
if (parent != NULL && child != NULL)
{
//connected case
if (parent->parent != NULL)
{
if (parent->parent->left == parent)
{
parent->parent->left = child;
}
else
{
parent->parent->right = child;
}
}
//update left/right for parent
if (parent->left != NULL && child != parent->left)
{
parent->left->parent = child;
}
if (parent->right != NULL && child != parent->right)
{
parent->right->parent = child;
}
//update left/right for children
if (child->left != NULL)
{
child->left->parent = parent;
}
if (child->right != NULL)
{
child->right->parent = parent;
}
child->parent = parent->parent;
parent->parent = child;
if (child == parent->left)
{
/* parent
/ \
children */
parent->left = child->left;
child->left = parent;
child->parent = parent->parent;
parent->parent = child;
if (child == parent->left)
{
/* parent
/ \
children */
parent->left = child->left;
child->left = parent;
temp = parent->right;
parent->right = child->right;
child->right = temp;
}
else
{
/* parent
/ \
children */
parent->right = child->right;
child->right = parent;
temp = parent->right;
parent->right = child->right;
child->right = temp;
}
else
{
/* parent
/ \
children */
parent->right = child->right;
child->right = parent;
temp = parent->left;
parent->left = child->left;
child->left = temp;
}
}
else
{
//not connected case
//adjust all the nodes other than node1,node2
if (node1->left != NULL)
node1->left->parent = node2;
if (node1->right != NULL)
node1->right->parent = node2;
temp = parent->left;
parent->left = child->left;
child->left = temp;
}
}
else
{
//not connected case
//adjust all the nodes other than node1,node2
if (node1->left != NULL)
{
node1->left->parent = node2;
}
if (node1->right != NULL)
{
node1->right->parent = node2;
}
if (node2->left != NULL)
node2->left->parent = node1;
if (node2->right != NULL)
node2->right->parent = node1;
if (node2->left != NULL)
{
node2->left->parent = node1;
}
if (node2->right != NULL)
{
node2->right->parent = node1;
}
if (node1->parent != NULL)
{
if (node1->parent->left == node1)
node1->parent->left = node2;
else
node1->parent->right = node2;
}
if (node1->parent != NULL)
{
if (node1->parent->left == node1)
{
node1->parent->left = node2;
}
else
{
node1->parent->right = node2;
}
}
if (node2->parent != NULL)
{
if (node2->parent->left == node2)
node2->parent->left = node1;
else
node2->parent->right = node1;
}
if (node2->parent != NULL)
{
if (node2->parent->left == node2)
{
node2->parent->left = node1;
}
else
{
node2->parent->right = node1;
}
}
//adjust node1,node2
temp = node1->parent;
node1->parent = node2->parent;
node2->parent = temp;
//adjust node1,node2
temp = node1->parent;
node1->parent = node2->parent;
node2->parent = temp;
temp = node1->left;
node1->left = node2->left;
node2->left = temp;
temp = node1->left;
node1->left = node2->left;
node2->left = temp;
temp = node1->right;
node1->right = node2->right;
node2->right = temp;
}
temp = node1->right;
node1->right = node2->right;
node2->right = temp;
}
//swap height
int32_t height = node1->height;
node1->height = node2->height;
node2->height = height;
return;
//swap height
int32_t height = node1->height;
node1->height = node2->height;
node2->height = height;
return;
}
static avl_tree_node_t *KABI lbp_avl_tree_node_delete(avl_tree_node_t *root,
@ -240,203 +293,253 @@ static avl_tree_node_t *KABI lbp_avl_tree_node_delete(avl_tree_node_t *root,
callback_func_t compare,
avl_tree_node_t **deleted_node)
{
if (root == NULL || node == NULL || compare == NULL || deleted_node == NULL)
return root;
const int32_t comp = compare(root, node);
if (comp < 0)
root->right = lbp_avl_tree_node_delete(root->right, node, compare, deleted_node);
else if (comp > 0)
root->left = lbp_avl_tree_node_delete(root->left, node, compare, deleted_node);
else
{
*deleted_node = root;
// node with only one child or no child
if ((root->left == NULL) || (root->right == NULL))
{
avl_tree_node_t *child = root->left != NULL ? root->left : root->right;
if (root == NULL || node == NULL || compare == NULL || deleted_node == NULL)
{
return root;
}
const int32_t comp = compare(root, node);
if (comp < 0)
{
root->right = lbp_avl_tree_node_delete(root->right, node, compare, deleted_node);
}
else
{
if (comp > 0)
{
root->left = lbp_avl_tree_node_delete(root->left, node, compare, deleted_node);
}
else
{
*deleted_node = root;
// node with only one child or no child
if ((root->left == NULL) || (root->right == NULL))
{
avl_tree_node_t *child = root->left != NULL ? root->left : root->right;
if (child == NULL)
{ // 0 child
root = NULL;
}
else // 1 child
{
child->parent = root->parent;
root = child;
}
}
else
{
// node with two children: Get the inorder successor (smallest
// in the right subtree)
avl_tree_node_t *successor = lb_avl_tree_larger(root);
//swap fields
lbp_avl_tree_swap_nodes(successor, root);
if (child == NULL)
{ // 0 child
root = NULL;
}
else // 1 child
{
child->parent = root->parent;
root = child;
}
}
else
{
// node with two children: Get the inorder successor (smallest
// in the right subtree)
avl_tree_node_t *successor = lb_avl_tree_larger(root);
//swap fields
lbp_avl_tree_swap_nodes(successor, root);
// Detach the inorder successor
successor->right = lbp_avl_tree_node_delete(successor->right, root, compare, deleted_node);
// Detach the inorder successor
successor->right = lbp_avl_tree_node_delete(successor->right, root, compare, deleted_node);
root = successor;
}
}
if (root == NULL)
return root;
root->height = lb_max_32(lbp_avl_tree_node_get_height(root->left), lbp_avl_tree_node_get_height(root->right)) + 1;
root = lbp_avl_tree_node_balance(root);
return root;
root = successor;
}
}
}
if (root == NULL)
{
return root;
}
root->height = lb_max_32(lbp_avl_tree_node_get_height(root->left), lbp_avl_tree_node_get_height(root->right)) + 1;
root = lbp_avl_tree_node_balance(root);
return root;
}
static avl_tree_node_t *KABI lbp_avl_tree_node_search(avl_tree_node_t *root, avl_tree_node_t *node,
callback_func_t compare)
{
if (root == NULL || compare == NULL)
return NULL;
const int32_t comp = compare(root, node);
if (comp < 0)
return lbp_avl_tree_node_search(root->right, node, compare);
else if (comp == 0)
return root;
else
return lbp_avl_tree_node_search(root->left, node, compare);
if (root == NULL || compare == NULL)
{
return NULL;
}
const int32_t comp = compare(root, node);
if (comp < 0)
{
return lbp_avl_tree_node_search(root->right, node, compare);
}
else
{
if (comp == 0)
{
return root;
}
else
{
return lbp_avl_tree_node_search(root->left, node, compare);
}
}
}
static void KABI lbp_avl_tree_node_init(avl_tree_node_t *it)
{
if (it != NULL)
{
it->height = 0;
it->left = NULL;
it->right = NULL;
it->parent = NULL;
}
return;
if (it != NULL)
{
it->height = 0;
it->left = NULL;
it->right = NULL;
it->parent = NULL;
}
return;
}
avl_tree_node_t *KABI lb_avl_tree_smallest(avl_tree_t *tree)
{
if (tree == NULL)
return NULL;
avl_tree_node_t *entry = tree->root;
if (entry == NULL)
return NULL;
while (entry->left != NULL)
entry = entry->left;
return entry;
if (tree == NULL)
{
return NULL;
}
avl_tree_node_t *entry = tree->root;
if (entry == NULL)
{
return NULL;
}
while (entry->left != NULL)
{
entry = entry->left;
}
return entry;
}
avl_tree_node_t *KABI lb_avl_tree_largest(avl_tree_t *tree)
{
if (tree == NULL)
return NULL;
avl_tree_node_t *entry = tree->root;
if (entry == NULL)
return NULL;
while (entry->right != NULL)
entry = entry->right;
return entry;
if (tree == NULL)
{
return NULL;
}
avl_tree_node_t *entry = tree->root;
if (entry == NULL)
{
return NULL;
}
while (entry->right != NULL)
{
entry = entry->right;
}
return entry;
}
avl_tree_node_t *KABI lb_avl_tree_larger(avl_tree_node_t *it)
{
if (it == NULL)
return NULL;
avl_tree_node_t *root = it;
if (root->right != NULL)
{
root = root->right;
while (root->left != NULL)
root = root->left;
return root;
}
else
{
while (root->parent != NULL)
{
if (root->parent->left == root)
return root->parent;
root = root->parent;
}
return NULL;
}
if (it == NULL)
{
return NULL;
}
avl_tree_node_t *root = it;
if (root->right != NULL)
{
root = root->right;
while (root->left != NULL)
{
root = root->left;
}
return root;
}
else
{
while (root->parent != NULL)
{
if (root->parent->left == root)
{
return root->parent;
}
root = root->parent;
}
return NULL;
}
}
avl_tree_node_t *KABI lb_avl_tree_smaller(avl_tree_node_t *it)
{
if (it == NULL)
return NULL;
avl_tree_node_t *root = it;
if (root->left != NULL)
{
root = root->left;
while (root->right != NULL)
root = root->right;
return root;
}
else
{
while (root->parent != NULL)
{
if (root->parent->right == root)
return root->parent;
root = root->parent;
}
return NULL;
}
if (it == NULL)
{
return NULL;
}
avl_tree_node_t *root = it;
if (root->left != NULL)
{
root = root->left;
while (root->right != NULL)
{
root = root->right;
}
return root;
}
else
{
while (root->parent != NULL)
{
if (root->parent->right == root)
{
return root->parent;
}
root = root->parent;
}
return NULL;
}
}
avl_tree_node_t *KABI lb_avl_tree_search(avl_tree_t *tree, avl_tree_node_t *node)
{
return lbp_avl_tree_node_search(tree->root, node, tree->compare);
return lbp_avl_tree_node_search(tree->root, node, tree->compare);
}
void KABI lb_avl_tree_insert(avl_tree_t *tree, avl_tree_node_t *data)
{
if (tree != NULL && data != NULL)
{
lbp_avl_tree_node_init(data);
tree->root = lbp_avl_tree_node_insert(tree->root, data, tree->compare, NULL);
}
return;
if (tree != NULL && data != NULL)
{
lbp_avl_tree_node_init(data);
tree->root = lbp_avl_tree_node_insert(tree->root, data, tree->compare, NULL);
}
return;
}
avl_tree_node_t *KABI lb_avl_tree_delete(avl_tree_t *tree, avl_tree_node_t *data)
{
avl_tree_node_t *node = NULL;
if (tree != NULL && data != NULL)
{
tree->root = lbp_avl_tree_node_delete(tree->root, data, tree->compare, &node);
}
return node;
avl_tree_node_t *node = NULL;
if (tree != NULL && data != NULL)
{
tree->root = lbp_avl_tree_node_delete(tree->root, data, tree->compare, &node);
}
return node;
}
int32_t KABI lb_avl_tree_size(avl_tree_t *tree)
{
if (tree == NULL)
return -1;
if (tree->root == NULL)
return 0;
int32_t size = 0;
avl_tree_node_t *entry = lb_avl_tree_smallest(tree);
while (entry != NULL)
{
size++;
entry = lb_avl_tree_larger(entry);
}
return size;
if (tree == NULL)
{
return -1;
}
if (tree->root == NULL)
{
return 0;
}
int32_t size = 0;
avl_tree_node_t *entry = lb_avl_tree_smallest(tree);
while (entry != NULL)
{
size++;
entry = lb_avl_tree_larger(entry);
}
return size;
}
void KABI lb_avl_tree_init(avl_tree_t *tree, callback_func_t compare)
{
if (tree != NULL)
{
tree->compare = compare;
tree->root = NULL;
}
return;
if (tree != NULL)
{
tree->compare = compare;
tree->root = NULL;
}
return;
}
@ -445,40 +548,55 @@ void KABI lb_avl_tree_init(avl_tree_t *tree, callback_func_t compare)
static int32_t KABI lbp_avl_tree_node_calculate_height(avl_tree_node_t *tree)
{
if (tree == NULL)
return -1;
return lb_max_32(lbp_avl_tree_node_calculate_height(tree->left), lbp_avl_tree_node_calculate_height(tree->right)) + 1;
if (tree == NULL)
{
return -1;
}
return lb_max_32(lbp_avl_tree_node_calculate_height(tree->left), lbp_avl_tree_node_calculate_height(tree->right)) +
1;
}
static bool KABI lbp_avl_tree_node_test(avl_tree_node_t *tree, callback_func_t compare)
{
if (tree == NULL)
return true;
if (lbp_avl_tree_node_get_balance_factor(tree) < -1 || lbp_avl_tree_node_get_balance_factor(tree) > 1 ||
lbp_avl_tree_node_calculate_height(tree) != tree->height)
return false;
if (tree->left != NULL)
{
if (tree->left->parent != tree)
return false;
}
if (tree->right != NULL)
{
if (tree->right->parent != tree)
return false;
}
if (compare != NULL)
{
if ((tree->right != NULL && compare(tree, tree->right) > 0) ||
(tree->left != NULL && compare(tree, tree->left) < 0))
return false;
}
return lbp_avl_tree_node_test(tree->left, compare) && lbp_avl_tree_node_test(tree->right, compare);
if (tree == NULL)
{
return true;
}
if (lbp_avl_tree_node_get_balance_factor(tree) < -1 || lbp_avl_tree_node_get_balance_factor(tree) > 1 ||
lbp_avl_tree_node_calculate_height(tree) != tree->height)
{
return false;
}
if (tree->left != NULL)
{
if (tree->left->parent != tree)
{
return false;
}
}
if (tree->right != NULL)
{
if (tree->right->parent != tree)
{
return false;
}
}
if (compare != NULL)
{
if ((tree->right != NULL && compare(tree, tree->right) > 0) ||
(tree->left != NULL && compare(tree, tree->left) < 0))
{
return false;
}
}
return lbp_avl_tree_node_test(tree->left, compare) && lbp_avl_tree_node_test(tree->right, compare);
}
bool KABI lb_avl_tree_validate(avl_tree_t *tree)
{
if (tree == NULL)
return true;
return lbp_avl_tree_node_test(tree->root, tree->compare);
if (tree == NULL)
{
return true;
}
return lbp_avl_tree_node_test(tree->root, tree->compare);
}

View File

@ -2,310 +2,343 @@
static void KABI lbp_init_linked_list_node(linked_list_node_t *node)
{
if (node != NULL)
{
node->next = NULL;
node->prev = NULL;
}
return;
if (node != NULL)
{
node->next = NULL;
node->prev = NULL;
}
return;
}
static void KABI lbp_append_node(linked_list_node_t *target, linked_list_node_t *node)
{
if(target == NULL || node == NULL)
return;
if (target == NULL || node == NULL)
{
return;
}
linked_list_node_t* next = target->next;
// update the next node
if(next != NULL)
{
next->prev = node;
}
linked_list_node_t *next = target->next;
// update the next node
if (next != NULL)
{
next->prev = node;
}
// update the target node
target->next = node;
// update the target node
target->next = node;
// update the node itself
node->prev = target;
node->next = next;
// update the node itself
node->prev = target;
node->next = next;
return;
return;
}
// link target with node, suppose target is in the current list
static void KABI lbp_prepend_node(linked_list_node_t *target, linked_list_node_t *node)
{
if(target == NULL || node == NULL)
return;
if (target == NULL || node == NULL)
{
return;
}
linked_list_node_t* prev = target->prev;
// update the prev node
if(prev != NULL)
{
prev->next = node;
}
linked_list_node_t *prev = target->prev;
// update the prev node
if (prev != NULL)
{
prev->next = node;
}
// update the target node
target->prev = node;
// update the target node
target->prev = node;
// update the node itself
node->next = target;
node->prev = prev;
// update the node itself
node->next = target;
node->prev = prev;
return;
return;
}
static void KABI lbp_unlink_node(linked_list_node_t *node)
{
if(node == NULL)
return;
if (node == NULL)
{
return;
}
if(node->prev != NULL)
{
node->prev->next = node->next;
}
if (node->prev != NULL)
{
node->prev->next = node->next;
}
if(node->next != NULL)
{
node->next->prev = node->prev;
}
if (node->next != NULL)
{
node->next->prev = node->prev;
}
return;
return;
}
void KABI lb_linked_list_init(linked_list_t *list)
{
if (list != NULL)
{
list->head = NULL;
list->tail = NULL;
}
return;
if (list != NULL)
{
list->head = NULL;
list->tail = NULL;
}
return;
}
int32_t KABI lb_linked_list_size(linked_list_t *list)
{
if (list == NULL)
return -1;
if (list->head == NULL)
return 0;
if (list == NULL)
{
return -1;
}
if (list->head == NULL)
{
return 0;
}
int32_t size = 1;
linked_list_node_t *cur_node = list->head;
linked_list_node_t *tail = list->tail;
while ((cur_node != tail) && ((cur_node = cur_node->next) != NULL))
{
size++;
}
return size;
int32_t size = 1;
linked_list_node_t *cur_node = list->head;
linked_list_node_t *tail = list->tail;
while ((cur_node != tail) && ((cur_node = cur_node->next) != NULL))
{
size++;
}
return size;
}
void KABI lb_linked_list_push_front(linked_list_t *list, linked_list_node_t *node)
{
if (list == NULL || node == NULL)
return;
if (list == NULL || node == NULL)
{
return;
}
lbp_init_linked_list_node(node);
lbp_init_linked_list_node(node);
lb_linked_list_insert_ref(list, NULL, node);
lb_linked_list_insert_ref(list, NULL, node);
return;
return;
}
void KABI lb_linked_list_push_back(linked_list_t *list, linked_list_node_t *node)
{
if (list == NULL || node == NULL)
return;
if (list == NULL || node == NULL)
{
return;
}
lbp_init_linked_list_node(node);
lbp_init_linked_list_node(node);
lb_linked_list_insert_ref(list, list->tail, node);
lb_linked_list_insert_ref(list, list->tail, node);
return;
return;
}
linked_list_node_t *KABI lb_linked_list_pop_front(linked_list_t *list)
{
if (list == NULL)
return NULL;
return lb_linked_list_remove_ref(list, list->head);
if (list == NULL)
{
return NULL;
}
return lb_linked_list_remove_ref(list, list->head);
}
linked_list_node_t *KABI lb_linked_list_pop_back(linked_list_t *list)
{
if (list == NULL)
return NULL;
if (list == NULL)
{
return NULL;
}
return lb_linked_list_remove_ref(list, list->tail);
return lb_linked_list_remove_ref(list, list->tail);
}
void KABI lb_linked_list_insert_ref(linked_list_t *list, linked_list_node_t *prev_node, linked_list_node_t *node)
{
if (list == NULL || node == NULL)
return;
lbp_init_linked_list_node(node);
if (prev_node == NULL)
{
// if prev_node is NULL, then we are inserting to the head
if (list == NULL || node == NULL)
{
return;
}
lbp_init_linked_list_node(node);
if (prev_node == NULL)
{
// if prev_node is NULL, then we are inserting to the head
// linked node with list->head
lbp_prepend_node(list->head, node);
// linked node with list->head
lbp_prepend_node(list->head, node);
if (list->tail == NULL)
{
// if the list is empty, we assign list->tail to node too
list->tail = node;
}
if (list->tail == NULL)
{
// if the list is empty, we assign list->tail to node too
list->tail = node;
}
list->head = node;
}
else
{
// if prev_node is not NULL, we are inserting to the middle or the end
list->head = node;
}
else
{
// if prev_node is not NULL, we are inserting to the middle or the end
// linked node with the prev_node
lbp_append_node(prev_node, node);
// linked node with the prev_node
lbp_append_node(prev_node, node);
if (node->next == NULL)
{
// if it's the end
list->tail = node;
}
}
if (node->next == NULL)
{
// if it's the end
list->tail = node;
}
}
}
void KABI lb_linked_list_insert(linked_list_t *list, int32_t index, linked_list_node_t *node)
{
if (list == NULL || index < 0 || node == NULL)
return;
linked_list_node_t *prev_node = lb_linked_list_get(list, index - 1);
lbp_init_linked_list_node(node);
if (list == NULL || index < 0 || node == NULL)
{
return;
}
linked_list_node_t *prev_node = lb_linked_list_get(list, index - 1);
lbp_init_linked_list_node(node);
if (prev_node == NULL)
{
if (index == 0)
{
lb_linked_list_insert_ref(list, NULL, node);
}
}
else
{
lb_linked_list_insert_ref(list, prev_node, node);
}
if (prev_node == NULL)
{
if (index == 0)
{
lb_linked_list_insert_ref(list, NULL, node);
}
}
else
{
lb_linked_list_insert_ref(list, prev_node, node);
}
return;
return;
}
linked_list_node_t *KABI lb_linked_list_remove(linked_list_t *list, int32_t index)
{
if (list == NULL || index < 0)
return NULL;
linked_list_node_t *cur_node = lb_linked_list_get(list, index);
if (list == NULL || index < 0)
{
return NULL;
}
linked_list_node_t *cur_node = lb_linked_list_get(list, index);
if (cur_node == NULL)
return NULL;
if (cur_node == NULL)
{
return NULL;
}
return lb_linked_list_remove_ref(list, cur_node);
return lb_linked_list_remove_ref(list, cur_node);
}
linked_list_node_t *KABI lb_linked_list_remove_ref(linked_list_t *list, linked_list_node_t *node)
{
if (list == NULL || node == NULL)
return NULL;
if (list == NULL || node == NULL)
{
return NULL;
}
lbp_unlink_node(node);
lbp_unlink_node(node);
if(node->next == NULL)
{
list->tail = node->prev;
}
if (node->next == NULL)
{
list->tail = node->prev;
}
if(node->prev == NULL)
{
list->head = node->next;
}
if (node->prev == NULL)
{
list->head = node->next;
}
lbp_init_linked_list_node(node);
lbp_init_linked_list_node(node);
return node;
return node;
}
linked_list_node_t *KABI lb_linked_list_get(linked_list_t *list, int32_t index)
{
if (list == NULL || index < 0 || list->head == NULL)
return NULL;
linked_list_node_t *cur_node = list->head;
while (index-- && (cur_node = cur_node->next) != NULL);
return cur_node;
if (list == NULL || index < 0 || list->head == NULL)
{
return NULL;
}
linked_list_node_t *cur_node = list->head;
while (index-- && (cur_node = cur_node->next) != NULL)
{}
return cur_node;
}
linked_list_node_t *KABI lb_linked_list_next(linked_list_node_t *node)
{
if (node != NULL)
{
node = node->next;
}
return node;
if (node != NULL)
{
node = node->next;
}
return node;
}
linked_list_node_t *KABI lb_linked_list_prev(linked_list_node_t *node)
{
if (node != NULL)
{
node = node->prev;
}
return node;
if (node != NULL)
{
node = node->prev;
}
return node;
}
linked_list_node_t *KABI lb_linked_list_first(linked_list_t *list)
{
linked_list_node_t *result = NULL;
if (list != NULL)
{
result = list->head;
}
return result;
linked_list_node_t *result = NULL;
if (list != NULL)
{
result = list->head;
}
return result;
}
linked_list_node_t *KABI lb_linked_list_last(linked_list_t *list)
{
linked_list_node_t *result = NULL;
if (list != NULL)
{
result = list->tail;
}
return result;
linked_list_node_t *result = NULL;
if (list != NULL)
{
result = list->tail;
}
return result;
}
int32_t KABI lb_linked_list_search(linked_list_t *list, linked_list_node_t *target,
callback_func_t equals)
{
if(list == NULL || target == NULL)
return -1;
int32_t result = 0;
linked_list_node_t* node = lb_linked_list_first(list);
while(node != NULL)
{
if(equals != NULL)
{
if (equals(node, target))
{
return result;
}
}
else
{
if(target->next == node->next && target->prev == node->prev)
{
return result;
}
}
result++;
node = lb_linked_list_next(node);
}
if (list == NULL || target == NULL)
{
return -1;
}
int32_t result = 0;
linked_list_node_t *node = lb_linked_list_first(list);
while (node != NULL)
{
if (equals != NULL)
{
if (equals(node, target))
{
return result;
}
}
else
{
if (target->next == node->next && target->prev == node->prev)
{
return result;
}
}
result++;
node = lb_linked_list_next(node);
}
return -1;
return -1;
}

View File

@ -3,8 +3,8 @@
typedef union
{
uint32_t size;
uint32_t flags;
uint32_t size;
uint32_t flags;
} _salloc_header;
#define ALLOC_FLAG_NUM 2
@ -13,207 +13,207 @@ typedef union
static void lbp_set_salloc_header_size(_salloc_header *header, uint32_t size)
{
// align the integer, ignoring overflowed bits
size <<= ALLOC_FLAG_NUM;
// align the integer, ignoring overflowed bits
size <<= ALLOC_FLAG_NUM;
// clear ALLOC_FLAG_NUM-th to 31-th bits
header->size &= ~(uint32_t) lb_bit_field_mask(ALLOC_FLAG_NUM, 31);
// set bits
header->size |= size;
return;
// clear ALLOC_FLAG_NUM-th to 31-th bits
header->size &= ~(uint32_t) lb_bit_field_mask(ALLOC_FLAG_NUM, 31);
// set bits
header->size |= size;
return;
}
static uint32_t lbp_read_salloc_header_size(_salloc_header *header)
{
return header->size >> ALLOC_FLAG_NUM;
return header->size >> ALLOC_FLAG_NUM;
}
static uint32_t lbp_read_salloc_header_flag(_salloc_header *header, uint32_t bit)
{
return (header->flags & (uint32_t) lb_bit_mask(bit)) == 0 ? 0 : 1;
return (header->flags & (uint32_t) lb_bit_mask(bit)) == 0 ? 0 : 1;
}
static void lbp_set_salloc_header_flag(_salloc_header *header, uint32_t bit, uint32_t value)
{
value &= (uint32_t) lb_bit_mask(0);
if (value == 1)
{
header->flags |= (uint32_t) lb_bit_mask(bit);
}
else
{
header->flags &= ~(uint32_t) lb_bit_mask(bit);
}
return;
value &= (uint32_t) lb_bit_mask(0);
if (value == 1)
{
header->flags |= (uint32_t) lb_bit_mask(bit);
}
else
{
header->flags &= ~(uint32_t) lb_bit_mask(bit);
}
return;
}
static void lbp_salloc_join(void *base)
{
if (base != NULL)
{
char *c_ptr = (char *) base;
while (1)
{
uint32_t c_blk_free = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE);
uint32_t c_blk_last = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST);
uint32_t c_blk_size = lbp_read_salloc_header_size((_salloc_header *) c_ptr);
char *n_ptr = c_blk_last == 1 ? NULL : c_ptr + c_blk_size;
if (n_ptr != NULL && c_blk_free == 1)
{
// if this is not the last block and the prev block is free
uint32_t n_blk_free = lbp_read_salloc_header_flag((_salloc_header *) n_ptr, ALLOC_HEADER_FLAG_FREE);
uint32_t n_blk_last = lbp_read_salloc_header_flag((_salloc_header *) n_ptr, ALLOC_HEADER_FLAG_LAST);
uint32_t n_blk_size = lbp_read_salloc_header_size((_salloc_header *) n_ptr);
if (base != NULL)
{
char *c_ptr = (char *) base;
while (1)
{
uint32_t c_blk_free = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE);
uint32_t c_blk_last = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST);
uint32_t c_blk_size = lbp_read_salloc_header_size((_salloc_header *) c_ptr);
char *n_ptr = c_blk_last == 1 ? NULL : c_ptr + c_blk_size;
if (n_ptr != NULL && c_blk_free == 1)
{
// if this is not the last block and the prev block is free
uint32_t n_blk_free = lbp_read_salloc_header_flag((_salloc_header *) n_ptr, ALLOC_HEADER_FLAG_FREE);
uint32_t n_blk_last = lbp_read_salloc_header_flag((_salloc_header *) n_ptr, ALLOC_HEADER_FLAG_LAST);
uint32_t n_blk_size = lbp_read_salloc_header_size((_salloc_header *) n_ptr);
if (n_blk_free == 1)
{
// logically gone
lbp_set_salloc_header_size((_salloc_header *) c_ptr, n_blk_size + c_blk_size);
lbp_set_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST, n_blk_last);
continue;
}
}
// update the c_ptr
if (c_blk_last == 0)
{
c_ptr += c_blk_size;
}
else
{
break;
}
}
}
if (n_blk_free == 1)
{
// logically gone
lbp_set_salloc_header_size((_salloc_header *) c_ptr, n_blk_size + c_blk_size);
lbp_set_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST, n_blk_last);
continue;
}
}
// update the c_ptr
if (c_blk_last == 0)
{
c_ptr += c_blk_size;
}
else
{
break;
}
}
}
}
bool KABI lb_salloc_assert(void *base, uint32_t *blk_size, bool *blk_free, uint32_t size)
{
if (base == NULL || blk_free == NULL || blk_size == NULL)
{
return NULL;
}
uint32_t i = 0;
char *c_ptr = (char *) base;
while (1)
{
uint32_t cur_blk_free = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE);
uint32_t cur_blk_last = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST);
uint32_t cur_blk_size = lbp_read_salloc_header_size((_salloc_header *) c_ptr);
if (cur_blk_free != blk_free[i] || cur_blk_size != blk_size[i])
{
return false;
}
else
{
c_ptr += cur_blk_size;
i++;
}
if (cur_blk_last == 1)
{
return i == size;
}
}
if (base == NULL || blk_free == NULL || blk_size == NULL)
{
return NULL;
}
uint32_t i = 0;
char *c_ptr = (char *) base;
while (1)
{
uint32_t cur_blk_free = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE);
uint32_t cur_blk_last = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST);
uint32_t cur_blk_size = lbp_read_salloc_header_size((_salloc_header *) c_ptr);
if (cur_blk_free != blk_free[i] || cur_blk_size != blk_size[i])
{
return false;
}
else
{
c_ptr += cur_blk_size;
i++;
}
if (cur_blk_last == 1)
{
return i == size;
}
}
}
void KABI lb_salloc_init(void *base, uint32_t size)
{
if (base != NULL && size >= sizeof(_salloc_header))
{
_salloc_header *ptr = (_salloc_header *) base;
lbp_set_salloc_header_size(ptr, size);
lbp_set_salloc_header_flag(ptr, ALLOC_HEADER_FLAG_FREE, 1);
lbp_set_salloc_header_flag(ptr, ALLOC_HEADER_FLAG_LAST, 1);
}
return;
if (base != NULL && size >= sizeof(_salloc_header))
{
_salloc_header *ptr = (_salloc_header *) base;
lbp_set_salloc_header_size(ptr, size);
lbp_set_salloc_header_flag(ptr, ALLOC_HEADER_FLAG_FREE, 1);
lbp_set_salloc_header_flag(ptr, ALLOC_HEADER_FLAG_LAST, 1);
}
return;
}
void *KABI lb_salloc(void *base, uint32_t size)
{
void *result = NULL;
if (base != NULL && size != 0)
{
uint32_t total_size = size + sizeof(_salloc_header);
char *c_ptr = (char *) base;
while (1)
{
uint32_t cur_blk_free = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE);
uint32_t cur_blk_size = lbp_read_salloc_header_size((_salloc_header *) c_ptr);
uint32_t cur_blk_last = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST);
if (cur_blk_free == 0 || cur_blk_size < total_size)
{
//if cur block not a free block
//or the current block size is less than the size we want
if (cur_blk_last == 1)
{
//if last one, break and fail.
break;
}
else
{
c_ptr += cur_blk_size;
}
}
else
{
// we have a free block with enough size
if (total_size == cur_blk_size ||
cur_blk_size - total_size < sizeof(_salloc_header))
{
// since the space left is not enough for salloc_header
// we alloc the whole block
lbp_set_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE, 0);
}
else
{
// we split the block here
// set properties for the first block
lbp_set_salloc_header_size((_salloc_header *) c_ptr, total_size);
lbp_set_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST, 0);
lbp_set_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE, 0);
void *result = NULL;
if (base != NULL && size != 0)
{
uint32_t total_size = size + sizeof(_salloc_header);
char *c_ptr = (char *) base;
while (1)
{
uint32_t cur_blk_free = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE);
uint32_t cur_blk_size = lbp_read_salloc_header_size((_salloc_header *) c_ptr);
uint32_t cur_blk_last = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST);
if (cur_blk_free == 0 || cur_blk_size < total_size)
{
//if cur block not a free block
//or the current block size is less than the size we want
if (cur_blk_last == 1)
{
//if last one, break and fail.
break;
}
else
{
c_ptr += cur_blk_size;
}
}
else
{
// we have a free block with enough size
if (total_size == cur_blk_size ||
cur_blk_size - total_size < sizeof(_salloc_header))
{
// since the space left is not enough for salloc_header
// we alloc the whole block
lbp_set_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE, 0);
}
else
{
// we split the block here
// set properties for the first block
lbp_set_salloc_header_size((_salloc_header *) c_ptr, total_size);
lbp_set_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST, 0);
lbp_set_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE, 0);
// set properties for the second block
lbp_set_salloc_header_size((_salloc_header *) (c_ptr + total_size), cur_blk_size - total_size);
lbp_set_salloc_header_flag((_salloc_header *) (c_ptr + total_size), ALLOC_HEADER_FLAG_LAST,
cur_blk_last);
lbp_set_salloc_header_flag((_salloc_header *) (c_ptr + total_size), ALLOC_HEADER_FLAG_FREE, 1);
}
// return the pointer, skip the alloc header
result = c_ptr + sizeof(_salloc_header);
break;
}
}
}
return result;
// set properties for the second block
lbp_set_salloc_header_size((_salloc_header *) (c_ptr + total_size), cur_blk_size - total_size);
lbp_set_salloc_header_flag((_salloc_header *) (c_ptr + total_size), ALLOC_HEADER_FLAG_LAST,
cur_blk_last);
lbp_set_salloc_header_flag((_salloc_header *) (c_ptr + total_size), ALLOC_HEADER_FLAG_FREE, 1);
}
// return the pointer, skip the alloc header
result = c_ptr + sizeof(_salloc_header);
break;
}
}
}
return result;
}
void KABI lb_sfree(void *base, void *ptr)
{
if (base != NULL && ptr != NULL)
{
char *c_ptr = (char *) base;
while (1)
{
uint32_t cur_blk_free = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE);
uint32_t cur_blk_last = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST);
uint32_t cur_blk_size = lbp_read_salloc_header_size((_salloc_header *) c_ptr);
if (cur_blk_free == 0 && ptr == c_ptr + sizeof(_salloc_header))
{
// we found the block, mark it as free
lbp_set_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE, 1);
// merge blocks
lbp_salloc_join(base);
break;
}
if (base != NULL && ptr != NULL)
{
char *c_ptr = (char *) base;
while (1)
{
uint32_t cur_blk_free = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE);
uint32_t cur_blk_last = lbp_read_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_LAST);
uint32_t cur_blk_size = lbp_read_salloc_header_size((_salloc_header *) c_ptr);
if (cur_blk_free == 0 && ptr == c_ptr + sizeof(_salloc_header))
{
// we found the block, mark it as free
lbp_set_salloc_header_flag((_salloc_header *) c_ptr, ALLOC_HEADER_FLAG_FREE, 1);
// merge blocks
lbp_salloc_join(base);
break;
}
if (cur_blk_last == 1)
{
break;
}
else
{
c_ptr += cur_blk_size;
}
}
}
return;
if (cur_blk_last == 1)
{
break;
}
else
{
c_ptr += cur_blk_size;
}
}
}
return;
}

View File

@ -3,53 +3,53 @@
void KABI lb_mem_copy(void *src, void *dst, uint64_t size)
{
if (src == NULL || dst == NULL)
{
return;
}
char *cSrc = (char *) src;
char *cDst = (char *) dst;
while (size--)
{
*(cDst++) = *(cSrc++);
}
return;
if (src == NULL || dst == NULL)
{
return;
}
char *cSrc = (char *) src;
char *cDst = (char *) dst;
while (size--)
{
*(cDst++) = *(cSrc++);
}
return;
}
void KABI lb_mem_set(void *src, uint8_t const val, uint64_t size)
{
if (src == NULL)
{
return;
}
while (size--)
{
*(uint8_t *)src = val;
src = (void*)((uintptr_t)src + 1);
}
return;
if (src == NULL)
{
return;
}
while (size--)
{
*(uint8_t *) src = val;
src = (void *) ((uintptr_t) src + 1);
}
return;
}
void KABI lb_mem_move(void *src, void *dst, uint64_t size)
{
if (src == NULL || dst == NULL)
{
return;
}
if (src >= dst)
{
lb_mem_copy(src, dst, size);
return;
}
src = (void*)((uintptr_t)src + size - 1);
dst = (void*)((uintptr_t)dst + size - 1);
while (size--)
{
*(char*)dst = *(char*)src;
dst = (void*)((uintptr_t)dst - 1);
src = (void*)((uintptr_t)src - 1);
}
return;
if (src == NULL || dst == NULL)
{
return;
}
if (src >= dst)
{
lb_mem_copy(src, dst, size);
return;
}
src = (void *) ((uintptr_t) src + size - 1);
dst = (void *) ((uintptr_t) dst + size - 1);
while (size--)
{
*(char *) dst = *(char *) src;
dst = (void *) ((uintptr_t) dst - 1);
src = (void *) ((uintptr_t) src - 1);
}
return;
}
//
@ -60,18 +60,18 @@ static uint32_t max = 16777215;
uint32_t KABI lb_rand(void)
{
seed = seed * 1103512986 + 29865;
return (unsigned int) (seed / 65536) % (max + 1);
seed = seed * 1103512986 + 29865;
return (unsigned int) (seed / 65536) % (max + 1);
}
void KABI lb_srand(uint32_t _seed)
{
seed = _seed;
seed = _seed;
}
void KABI lb_mrand(uint32_t _max)
{
max = _max;
max = _max;
}
//
@ -80,36 +80,36 @@ void KABI lb_mrand(uint32_t _max)
uint64_t KABI lb_str_len(char const *str)
{
uint64_t length = 0;
if (str == NULL)
{
return 0;
}
while (*str != 0)
{
str++;
length++;
}
return length;
uint64_t length = 0;
if (str == NULL)
{
return 0;
}
while (*str != 0)
{
str++;
length++;
}
return length;
}
uint64_t KABI lb_str_cmp(char const *str1, char const *str2)
{
if (str1 == NULL || str2 == NULL)
{
return 0;
}
uint64_t length = lb_str_len(str1);
if (length != lb_str_len(str2))
{
return 0;
}
while (length--)
{
if (*(str1 + length) != *(str2 + length))
{
return 0;
}
}
return 1;
if (str1 == NULL || str2 == NULL)
{
return 0;
}
uint64_t length = lb_str_len(str1);
if (length != lb_str_len(str2))
{
return 0;
}
while (length--)
{
if (*(str1 + length) != *(str2 + length))
{
return 0;
}
}
return 1;
}

File diff suppressed because it is too large Load Diff

View File

@ -7,9 +7,9 @@
typedef struct
{
char *case_name;
bool success;
bool used;
char *case_name;
bool success;
bool used;
} case_info;
static case_info ginfo[CASE_NUM];
@ -18,142 +18,142 @@ static char *test_name;
static void test_info(void)
{
ke_printf("[TD-INFO][%s] - ", test_name);
ke_printf("[TD-INFO][%s] - ", test_name);
}
static void test_warning(void)
{
ke_printf("[TD-WARN][%s] - ", test_name);
ke_printf("[TD-WARN][%s] - ", test_name);
}
static void test_error(void)
{
ke_printf("[TD-ERR][%s] - ", test_name);
ke_printf("[TD-ERR][%s] - ", test_name);
}
static void gat_push(void *ptr)
{
for (int i = 0; i < GAT_SIZE; i++)
{
if (gat[i] == NULL)
{
gat[i] = ptr;
return;
}
}
for (int i = 0; i < GAT_SIZE; i++)
{
if (gat[i] == NULL)
{
gat[i] = ptr;
return;
}
}
}
static bool gat_full(void)
{
for (int i = 0; i < GAT_SIZE; i++)
{
if (gat[i] == NULL)
{
return false;
}
}
return true;
for (int i = 0; i < GAT_SIZE; i++)
{
if (gat[i] == NULL)
{
return false;
}
}
return true;
}
static void gat_free(void)
{
for (int i = 0; i < GAT_SIZE; i++)
{
if (gat[i] != NULL)
{
ke_free(gat[i]);
gat[i] = NULL;
}
}
for (int i = 0; i < GAT_SIZE; i++)
{
if (gat[i] != NULL)
{
ke_free(gat[i]);
gat[i] = NULL;
}
}
}
static void ginfo_push(char *case_name, bool success)
{
char *r_case_name = (case_name == NULL ? "Anonymous Case" : case_name);
for (int i = 0; i < CASE_NUM; i++)
{
if (!ginfo[i].used)
{
ginfo[i].case_name = r_case_name;
ginfo[i].success = success;
ginfo[i].used = true;
return;
}
}
test_warning();
ke_printf("GINFO full, [%s] result not recorded.\n", r_case_name);
char *r_case_name = (case_name == NULL ? "Anonymous Case" : case_name);
for (int i = 0; i < CASE_NUM; i++)
{
if (!ginfo[i].used)
{
ginfo[i].case_name = r_case_name;
ginfo[i].success = success;
ginfo[i].used = true;
return;
}
}
test_warning();
ke_printf("GINFO full, [%s] result not recorded.\n", r_case_name);
}
void KABI test_begin(char *name)
{
test_name = (name == NULL ? "Anonymous Test" : name);
for (int i = 0; i < GAT_SIZE; i++)
{
gat[i] = NULL;
}
for (int i = 0; i < CASE_NUM; i++)
{
ginfo[i].used = false;
}
test_name = (name == NULL ? "Anonymous Test" : name);
for (int i = 0; i < GAT_SIZE; i++)
{
gat[i] = NULL;
}
for (int i = 0; i < CASE_NUM; i++)
{
ginfo[i].used = false;
}
}
void KABI test_end(void)
{
gat_free();
int32_t total = 0, failed = 0, success = 0;
for (int i = 0; i < CASE_NUM; i++)
{
if (ginfo[i].used)
{
total++;
if (ginfo[i].success)
{
success++;
}
else
{
failed++;
}
}
}
test_info();
ke_printf("%s\n", failed > 0 ? "FAIL" : "PASS");
ke_printf(" %d cases executed. S: %d. F: %d.\n", total, success, failed);
if (failed > 0)
{
for (int i = 0; i < CASE_NUM; i++)
{
if (ginfo[i].used && !ginfo[i].success)
{
ke_printf(" %s FAILED\n", ginfo[i].case_name);
}
}
}
for (int i = 0; i < CASE_NUM; i++)
{
ginfo[i].used = false;
}
gat_free();
int32_t total = 0, failed = 0, success = 0;
for (int i = 0; i < CASE_NUM; i++)
{
if (ginfo[i].used)
{
total++;
if (ginfo[i].success)
{
success++;
}
else
{
failed++;
}
}
}
test_info();
ke_printf("%s\n", failed > 0 ? "FAIL" : "PASS");
ke_printf(" %d cases executed. S: %d. F: %d.\n", total, success, failed);
if (failed > 0)
{
for (int i = 0; i < CASE_NUM; i++)
{
if (ginfo[i].used && !ginfo[i].success)
{
ke_printf(" %s FAILED\n", ginfo[i].case_name);
}
}
}
for (int i = 0; i < CASE_NUM; i++)
{
ginfo[i].used = false;
}
}
void KABI *talloc(uint32_t size)
{
if (!gat_full())
{
void *result = ke_alloc(size);
gat_push(result);
return result;
}
else
{
test_error();
ke_printf("GAT full, rejecting further allocations.\n");
}
return NULL;
if (!gat_full())
{
void *result = ke_alloc(size);
gat_push(result);
return result;
}
else
{
test_error();
ke_printf("GAT full, rejecting further allocations.\n");
}
return NULL;
}
void KABI run_case(char *name, bool result)
{
ginfo_push(name, result);
ginfo_push(name, result);
}

View File

@ -5,73 +5,75 @@
typedef struct
{
linked_list_node_t lnode;
int val;
linked_list_node_t lnode;
int val;
} my_list_node;
static bool validate_list(linked_list_t *list)
{
bool result = true;
// list_head_test
if (list->head != NULL)
{
result = result && (list->head->prev == NULL);
}
else
{
result = result && (list->tail == NULL);
}
bool result = true;
// list_head_test
if (list->head != NULL)
{
result = result && (list->head->prev == NULL);
}
else
{
result = result && (list->tail == NULL);
}
if (list->tail != NULL)
{
result = result && (list->tail->next == NULL);
}
else
{
result = result && (list->head == NULL);
}
if (list->tail != NULL)
{
result = result && (list->tail->next == NULL);
}
else
{
result = result && (list->head == NULL);
}
return result;
return result;
}
static bool assert_list(linked_list_t *list, int val[], int size)
{
linked_list_node_t *node = lb_linked_list_first(list);
int i = 0;
linked_list_node_t *node = lb_linked_list_first(list);
int i = 0;
if (!validate_list(list))
return false;
if (!validate_list(list))
{
return false;
}
while (node != NULL && i < size)
{
my_list_node *enode = OBTAIN_STRUCT_ADDR(node, my_list_node, lnode);
if (enode->val != val[i])
{
return false;
}
i++;
node = lb_linked_list_next(node);
}
while (node != NULL && i < size)
{
my_list_node *enode = OBTAIN_STRUCT_ADDR(node, my_list_node, lnode);
if (enode->val != val[i])
{
return false;
}
i++;
node = lb_linked_list_next(node);
}
if (i != size)
{
return false;
}
if (i != size)
{
return false;
}
node = lb_linked_list_last(list);
while (node != NULL && i >= 0)
{
my_list_node *enode = OBTAIN_STRUCT_ADDR(node, my_list_node, lnode);
if (enode->val != val[i - 1])
{
return false;
}
i--;
node = lb_linked_list_prev(node);
}
node = lb_linked_list_last(list);
while (node != NULL && i >= 0)
{
my_list_node *enode = OBTAIN_STRUCT_ADDR(node, my_list_node, lnode);
if (enode->val != val[i - 1])
{
return false;
}
i--;
node = lb_linked_list_prev(node);
}
return i == 0;
return i == 0;
}
//void print_validate(linked_list_t *list)
@ -98,366 +100,366 @@ static bool assert_list(linked_list_t *list, int val[], int size)
static void insert_val(linked_list_t *list, int index, int val)
{
my_list_node *a = (my_list_node *) talloc(sizeof(my_list_node));
a->val = val;
lb_linked_list_insert(list, index, &a->lnode);
my_list_node *a = (my_list_node *) talloc(sizeof(my_list_node));
a->val = val;
lb_linked_list_insert(list, index, &a->lnode);
}
static void push_back_val(linked_list_t *list, int val)
{
my_list_node *a = (my_list_node *) talloc(sizeof(my_list_node));
a->val = val;
lb_linked_list_push_back(list, &a->lnode);
my_list_node *a = (my_list_node *) talloc(sizeof(my_list_node));
a->val = val;
lb_linked_list_push_back(list, &a->lnode);
}
static void push_front_val(linked_list_t *list, int val)
{
my_list_node *a = (my_list_node *) talloc(sizeof(my_list_node));
a->val = val;
lb_linked_list_push_front(list, &a->lnode);
my_list_node *a = (my_list_node *) talloc(sizeof(my_list_node));
a->val = val;
lb_linked_list_push_front(list, &a->lnode);
}
static bool insert_test_beginning(void)
{
linked_list_t list;
lb_linked_list_init(&list);
insert_val(&list, 0, 0);
insert_val(&list, 0, 1);
insert_val(&list, 0, 2);
insert_val(&list, 0, 3);
linked_list_t list;
lb_linked_list_init(&list);
insert_val(&list, 0, 0);
insert_val(&list, 0, 1);
insert_val(&list, 0, 2);
insert_val(&list, 0, 3);
// 3210==0123
int val[4] = {3, 2, 1, 0};
return assert_list(&list, val, 4);
// 3210==0123
int val[4] = {3, 2, 1, 0};
return assert_list(&list, val, 4);
}
static bool insert_test_middle(void)
{
linked_list_t list;
lb_linked_list_init(&list);
linked_list_t list;
lb_linked_list_init(&list);
insert_val(&list, 0, 0);
insert_val(&list, 0, 1);
insert_val(&list, 0, 2);
insert_val(&list, 0, 0);
insert_val(&list, 0, 1);
insert_val(&list, 0, 2);
insert_val(&list, 1, 4);
insert_val(&list, 1, 5);
insert_val(&list, 2, 6);
insert_val(&list, 1, 4);
insert_val(&list, 1, 5);
insert_val(&list, 2, 6);
int val[] = {2, 5, 6, 4, 1, 0};
return assert_list(&list, val, 6);
int val[] = {2, 5, 6, 4, 1, 0};
return assert_list(&list, val, 6);
}
static bool insert_test_end(void)
{
linked_list_t list;
lb_linked_list_init(&list);
linked_list_t list;
lb_linked_list_init(&list);
insert_val(&list, 0, 0);
insert_val(&list, 1, 1);
insert_val(&list, 2, 2);
insert_val(&list, 3, 3);
insert_val(&list, 0, 0);
insert_val(&list, 1, 1);
insert_val(&list, 2, 2);
insert_val(&list, 3, 3);
int val[] = {0, 1, 2, 3};
return assert_list(&list, val, 4);
int val[] = {0, 1, 2, 3};
return assert_list(&list, val, 4);
}
static bool insert_test_invalid(void)
{
linked_list_t list;
lb_linked_list_init(&list);
linked_list_t list;
lb_linked_list_init(&list);
insert_val(&list, 0, 3);
insert_val(&list, 0, 2);
insert_val(&list, 0, 1);
insert_val(&list, 0, 0);
insert_val(&list, 0, 3);
insert_val(&list, 0, 2);
insert_val(&list, 0, 1);
insert_val(&list, 0, 0);
// large index
insert_val(&list, 5, 9);
insert_val(&list, 6, 9);
insert_val(&list, 999, 9);
// large index
insert_val(&list, 5, 9);
insert_val(&list, 6, 9);
insert_val(&list, 999, 9);
// small index
insert_val(&list, -1, 8);
insert_val(&list, -2, 8);
insert_val(&list, -999, 8);
// small index
insert_val(&list, -1, 8);
insert_val(&list, -2, 8);
insert_val(&list, -999, 8);
// NULL
insert_val(NULL, 1, 4);
lb_linked_list_insert_ref(NULL, list.head, list.tail);
lb_linked_list_insert_ref(&list, list.head, NULL);
// NULL
insert_val(NULL, 1, 4);
lb_linked_list_insert_ref(NULL, list.head, list.tail);
lb_linked_list_insert_ref(&list, list.head, NULL);
int val[] = {0, 1, 2, 3};
return assert_list(&list, val, 4);
int val[] = {0, 1, 2, 3};
return assert_list(&list, val, 4);
}
static bool remove_test_beginning(void)
{
linked_list_t list;
lb_linked_list_init(&list);
insert_val(&list, 0, 0);
insert_val(&list, 0, 1);
insert_val(&list, 0, 2);
insert_val(&list, 0, 3);
linked_list_t list;
lb_linked_list_init(&list);
insert_val(&list, 0, 0);
insert_val(&list, 0, 1);
insert_val(&list, 0, 2);
insert_val(&list, 0, 3);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove(&list, 0);
// 10==01
int val[] = {1, 0};
return assert_list(&list, val, 2);
// 10==01
int val[] = {1, 0};
return assert_list(&list, val, 2);
}
static bool remove_test_middle(void)
{
linked_list_t list;
lb_linked_list_init(&list);
linked_list_t list;
lb_linked_list_init(&list);
insert_val(&list, 0, 0);
insert_val(&list, 0, 1);
insert_val(&list, 0, 2);
insert_val(&list, 0, 0);
insert_val(&list, 0, 1);
insert_val(&list, 0, 2);
insert_val(&list, 0, 3);
insert_val(&list, 0, 4);
insert_val(&list, 0, 5);
insert_val(&list, 0, 3);
insert_val(&list, 0, 4);
insert_val(&list, 0, 5);
lb_linked_list_remove(&list, 1);
lb_linked_list_remove(&list, 2);
lb_linked_list_remove(&list, 1);
lb_linked_list_remove(&list, 2);
// 5310=====0135
int val[] = {5, 3, 1, 0};
return assert_list(&list, val, 4);
// 5310=====0135
int val[] = {5, 3, 1, 0};
return assert_list(&list, val, 4);
}
static bool remove_test_end(void)
{
linked_list_t list;
lb_linked_list_init(&list);
linked_list_t list;
lb_linked_list_init(&list);
insert_val(&list, 0, 0);
insert_val(&list, 1, 1);
insert_val(&list, 2, 2);
insert_val(&list, 3, 3);
insert_val(&list, 0, 0);
insert_val(&list, 1, 1);
insert_val(&list, 2, 2);
insert_val(&list, 3, 3);
lb_linked_list_remove(&list, 3);
lb_linked_list_remove(&list, 2);
lb_linked_list_remove(&list, 3);
lb_linked_list_remove(&list, 2);
int val[] = {0, 1};
return assert_list(&list, val, 2);
int val[] = {0, 1};
return assert_list(&list, val, 2);
}
static bool remove_test_all(void)
{
bool result = true;
linked_list_t list;
lb_linked_list_init(&list);
bool result = true;
linked_list_t list;
lb_linked_list_init(&list);
insert_val(&list, 0, 0);
insert_val(&list, 1, 1);
insert_val(&list, 2, 2);
insert_val(&list, 3, 3);
insert_val(&list, 0, 0);
insert_val(&list, 1, 1);
insert_val(&list, 2, 2);
insert_val(&list, 3, 3);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove(&list, 0);
result = result && assert_list(&list, NULL, 0);
result = result && assert_list(&list, NULL, 0);
insert_val(&list, 0, 0);
insert_val(&list, 1, 1);
insert_val(&list, 2, 2);
insert_val(&list, 3, 3);
insert_val(&list, 0, 0);
insert_val(&list, 1, 1);
insert_val(&list, 2, 2);
insert_val(&list, 3, 3);
lb_linked_list_remove(&list, 3);
lb_linked_list_remove(&list, 2);
lb_linked_list_remove(&list, 1);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove(&list, 3);
lb_linked_list_remove(&list, 2);
lb_linked_list_remove(&list, 1);
lb_linked_list_remove(&list, 0);
result = result && assert_list(&list, NULL, 0);
result = result && assert_list(&list, NULL, 0);
insert_val(&list, 0, 0);
insert_val(&list, 1, 1);
insert_val(&list, 2, 2);
insert_val(&list, 3, 3);
insert_val(&list, 0, 0);
insert_val(&list, 1, 1);
insert_val(&list, 2, 2);
insert_val(&list, 3, 3);
lb_linked_list_remove(&list, 1);
lb_linked_list_remove(&list, 1);
lb_linked_list_remove(&list, 1);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove(&list, 1);
lb_linked_list_remove(&list, 1);
lb_linked_list_remove(&list, 1);
lb_linked_list_remove(&list, 0);
result = result && assert_list(&list, NULL, 0);
result = result && assert_list(&list, NULL, 0);
return result;
return result;
}
static bool remove_test_invalid(void)
{
linked_list_t list;
lb_linked_list_init(&list);
linked_list_t list;
lb_linked_list_init(&list);
insert_val(&list, 0, 3);
insert_val(&list, 0, 2);
insert_val(&list, 0, 1);
insert_val(&list, 0, 0);
insert_val(&list, 0, 3);
insert_val(&list, 0, 2);
insert_val(&list, 0, 1);
insert_val(&list, 0, 0);
// large index
lb_linked_list_remove(&list, 5);
lb_linked_list_remove(&list, 6);
lb_linked_list_remove(&list, 999);
// large index
lb_linked_list_remove(&list, 5);
lb_linked_list_remove(&list, 6);
lb_linked_list_remove(&list, 999);
// small index
lb_linked_list_remove(&list, -1);
lb_linked_list_remove(&list, -2);
lb_linked_list_remove(&list, -999);
// small index
lb_linked_list_remove(&list, -1);
lb_linked_list_remove(&list, -2);
lb_linked_list_remove(&list, -999);
// NULL
lb_linked_list_remove(NULL, 1);
lb_linked_list_remove_ref(NULL, list.head);
lb_linked_list_remove_ref(&list, NULL);
// NULL
lb_linked_list_remove(NULL, 1);
lb_linked_list_remove_ref(NULL, list.head);
lb_linked_list_remove_ref(&list, NULL);
// 0123=====3210
int val[] = {0, 1, 2, 3};
return assert_list(&list, val, 4);
// 0123=====3210
int val[] = {0, 1, 2, 3};
return assert_list(&list, val, 4);
}
static bool size_test(void)
{
bool result = true;
linked_list_t list;
lb_linked_list_init(&list);
linked_list_t list2;
lb_linked_list_init(&list2);
bool result = true;
linked_list_t list;
lb_linked_list_init(&list);
linked_list_t list2;
lb_linked_list_init(&list2);
insert_val(&list, 0, 0);
insert_val(&list, 1, 1);
insert_val(&list, 2, 2);
insert_val(&list, 3, 3);
insert_val(&list, 0, 0);
insert_val(&list, 1, 1);
insert_val(&list, 2, 2);
insert_val(&list, 3, 3);
result = result && (lb_linked_list_size(&list) == 4 && lb_linked_list_size(&list2) == 0 &&
lb_linked_list_size(NULL) == -1);
int val[] = {0, 1, 2, 3};
result = result && assert_list(&list, val, 4);
return result;
result = result && (lb_linked_list_size(&list) == 4 && lb_linked_list_size(&list2) == 0 &&
lb_linked_list_size(NULL) == -1);
int val[] = {0, 1, 2, 3};
result = result && assert_list(&list, val, 4);
return result;
}
static bool push_pop_front_test(void)
{
bool result = true;
linked_list_t list;
lb_linked_list_init(&list);
bool result = true;
linked_list_t list;
lb_linked_list_init(&list);
push_front_val(&list, 1);
push_front_val(&list, 2);
push_front_val(&list, 3);
push_front_val(&list, 4);
push_front_val(&list, 1);
push_front_val(&list, 2);
push_front_val(&list, 3);
push_front_val(&list, 4);
//4321==1234
int val1[] = {4, 3, 2, 1};
result = result && assert_list(&list, val1, 4);
//4321==1234
int val1[] = {4, 3, 2, 1};
result = result && assert_list(&list, val1, 4);
lb_linked_list_pop_front(&list);
//321==123
int val2[] = {3, 2, 1};
result = result && assert_list(&list, val2, 3);
lb_linked_list_pop_front(&list);
//321==123
int val2[] = {3, 2, 1};
result = result && assert_list(&list, val2, 3);
lb_linked_list_pop_front(&list);
lb_linked_list_pop_front(&list);
lb_linked_list_pop_front(&list);
lb_linked_list_pop_front(&list);
lb_linked_list_pop_front(&list);
lb_linked_list_pop_front(&list);
result = result && assert_list(&list, NULL, 0);
return result;
result = result && assert_list(&list, NULL, 0);
return result;
}
static bool push_pop_back_test(void)
{
bool result = true;
linked_list_t list;
lb_linked_list_init(&list);
bool result = true;
linked_list_t list;
lb_linked_list_init(&list);
push_back_val(&list, 1);
push_back_val(&list, 2);
push_back_val(&list, 3);
push_back_val(&list, 4);
push_back_val(&list, 1);
push_back_val(&list, 2);
push_back_val(&list, 3);
push_back_val(&list, 4);
//1234==4321
int val1[] = {1, 2, 3, 4};
result = result && assert_list(&list, val1, 4);
//1234==4321
int val1[] = {1, 2, 3, 4};
result = result && assert_list(&list, val1, 4);
lb_linked_list_pop_back(&list);
//123==321
int val2[] = {1, 2, 3};
result = result && assert_list(&list, val2, 3);
lb_linked_list_pop_back(&list);
//123==321
int val2[] = {1, 2, 3};
result = result && assert_list(&list, val2, 3);
lb_linked_list_pop_back(&list);
lb_linked_list_pop_back(&list);
lb_linked_list_pop_back(&list);
lb_linked_list_pop_back(&list);
lb_linked_list_pop_back(&list);
lb_linked_list_pop_back(&list);
result = result && assert_list(&list, NULL, 0);
return result;
result = result && assert_list(&list, NULL, 0);
return result;
}
static int32_t equals(void *a, void *b)
{
return ((int64_t) (linked_list_node_t*)b) ==
OBTAIN_STRUCT_ADDR((linked_list_node_t*)a, my_list_node, lnode)->val;
return ((int64_t) (linked_list_node_t *) b) ==
OBTAIN_STRUCT_ADDR((linked_list_node_t *) a, my_list_node, lnode)->val;
}
static bool search_test(void)
{
bool result = true;
linked_list_t list;
lb_linked_list_init(&list);
bool result = true;
linked_list_t list;
lb_linked_list_init(&list);
push_back_val(&list, 1);
push_back_val(&list, 2);
push_back_val(&list, 3);
push_back_val(&list, 4);
push_back_val(&list, 1);
push_back_val(&list, 2);
push_back_val(&list, 3);
push_back_val(&list, 4);
int val1[] = {1, 2, 3, 4};
result = result && assert_list(&list, val1, 4);
int val1[] = {1, 2, 3, 4};
result = result && assert_list(&list, val1, 4);
result = result && (lb_linked_list_search(&list, (void *) 4, equals) == 3);
result = result && (lb_linked_list_search(&list, (linked_list_node_t *) 3, equals) == 2);
result = result && (lb_linked_list_search(&list, (linked_list_node_t *) 2, equals) == 1);
result = result && (lb_linked_list_search(&list, (linked_list_node_t *) 1, equals) == 0);
result = result && (lb_linked_list_search(&list, (void *) 4, equals) == 3);
result = result && (lb_linked_list_search(&list, (linked_list_node_t *) 3, equals) == 2);
result = result && (lb_linked_list_search(&list, (linked_list_node_t *) 2, equals) == 1);
result = result && (lb_linked_list_search(&list, (linked_list_node_t *) 1, equals) == 0);
result = result && (lb_linked_list_search(&list, NULL, equals) == -1);
result = result && (lb_linked_list_search(NULL, (linked_list_node_t *) 1, equals) == -1);
result = result && (lb_linked_list_search(&list, NULL, equals) == -1);
result = result && (lb_linked_list_search(NULL, (linked_list_node_t *) 1, equals) == -1);
linked_list_node_t *node = lb_linked_list_get(&list, 1);
result = result && (lb_linked_list_search(&list, node, NULL) == 1);
linked_list_node_t *node = lb_linked_list_get(&list, 1);
result = result && (lb_linked_list_search(&list, node, NULL) == 1);
result = result && assert_list(&list, val1, 4);
result = result && assert_list(&list, val1, 4);
return result;
return result;
}
void KABI linked_list_test(void)
{
test_begin("Linked list test");
run_case("insert_test_beginning", insert_test_beginning());
run_case("insert_test_middle", insert_test_middle());
run_case("insert_test_end", insert_test_end());
run_case("insert_test_invalid", insert_test_invalid());
test_begin("Linked list test");
run_case("insert_test_beginning", insert_test_beginning());
run_case("insert_test_middle", insert_test_middle());
run_case("insert_test_end", insert_test_end());
run_case("insert_test_invalid", insert_test_invalid());
run_case("remove_test_beginning", remove_test_beginning());
run_case("remove_test_middle", remove_test_middle());
run_case("remove_test_end", remove_test_end());
run_case("remove_test_invalid", remove_test_invalid());
run_case("remove_test_beginning", remove_test_beginning());
run_case("remove_test_middle", remove_test_middle());
run_case("remove_test_end", remove_test_end());
run_case("remove_test_invalid", remove_test_invalid());
run_case("size_test", size_test());
run_case("size_test", size_test());
run_case("remove_test_all", remove_test_all());
run_case("remove_test_all", remove_test_all());
run_case("push_pop_front_test", push_pop_front_test());
run_case("push_pop_back_test", push_pop_back_test());
run_case("push_pop_front_test", push_pop_front_test());
run_case("push_pop_back_test", push_pop_back_test());
run_case("search_test", search_test());
test_end();
run_case("search_test", search_test());
test_end();
}

View File

@ -3,8 +3,8 @@
typedef union
{
uint32_t size;
uint32_t flags;
uint32_t size;
uint32_t flags;
} _salloc_header;
const uint32_t salloc_header_size = sizeof(_salloc_header);
@ -13,269 +13,269 @@ static char buffer[1024];
static bool salloc_init_test(void)
{
lb_salloc_init(buffer, 1024);
uint32_t blk_size[] = {1024};
bool blk_free[] = {true};
return lb_salloc_assert(buffer, blk_size, blk_free, 1);
lb_salloc_init(buffer, 1024);
uint32_t blk_size[] = {1024};
bool blk_free[] = {true};
return lb_salloc_assert(buffer, blk_size, blk_free, 1);
}
static bool salloc_basic_alloc(void)
{
bool result = true;
lb_salloc_init(buffer, 1024);
result = result && (lb_salloc(buffer, 10) != NULL);
uint32_t blk_size[] = {10 + salloc_header_size, 1024-10-salloc_header_size};
bool blk_free[] = {false,true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 2);
return result;
bool result = true;
lb_salloc_init(buffer, 1024);
result = result && (lb_salloc(buffer, 10) != NULL);
uint32_t blk_size[] = {10 + salloc_header_size, 1024 - 10 - salloc_header_size};
bool blk_free[] = {false, true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 2);
return result;
}
static bool salloc_full_alloc(void)
{
bool result = true;
lb_salloc_init(buffer, 1024);
result = result && (lb_salloc(buffer, 1024 - salloc_header_size) != NULL);
uint32_t blk_size[] = {1024};
bool blk_free[] = {false};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
return result;
bool result = true;
lb_salloc_init(buffer, 1024);
result = result && (lb_salloc(buffer, 1024 - salloc_header_size) != NULL);
uint32_t blk_size[] = {1024};
bool blk_free[] = {false};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
return result;
}
static bool salloc_overflow_alloc(void)
{
bool result = true;
lb_salloc_init(buffer, 1024);
result = result && (lb_salloc(buffer, 1024 - salloc_header_size + 1) == NULL);
uint32_t blk_size[] = {1024};
bool blk_free[] = {true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
return result;
bool result = true;
lb_salloc_init(buffer, 1024);
result = result && (lb_salloc(buffer, 1024 - salloc_header_size + 1) == NULL);
uint32_t blk_size[] = {1024};
bool blk_free[] = {true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
return result;
}
static bool salloc_multiple_alloc(void)
{
bool result = true;
lb_salloc_init(buffer, 1024);
result = result && (lb_salloc(buffer, 10) != NULL);
result = result && (lb_salloc(buffer, 10) != NULL);
result = result && (lb_salloc(buffer, 10) != NULL);
uint32_t blk_size[] = {10 + salloc_header_size,
10 + salloc_header_size,
10 + salloc_header_size,
1024-3*(10+salloc_header_size)};
bool blk_free[] = {false,false,false,true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 4);
return result;
bool result = true;
lb_salloc_init(buffer, 1024);
result = result && (lb_salloc(buffer, 10) != NULL);
result = result && (lb_salloc(buffer, 10) != NULL);
result = result && (lb_salloc(buffer, 10) != NULL);
uint32_t blk_size[] = {10 + salloc_header_size,
10 + salloc_header_size,
10 + salloc_header_size,
1024 - 3 * (10 + salloc_header_size)};
bool blk_free[] = {false, false, false, true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 4);
return result;
}
static bool salloc_alloc_not_enough(void)
{
void* ptr;
bool result = true;
lb_salloc_init(buffer, salloc_header_size + salloc_header_size + salloc_header_size - 1);
ptr = lb_salloc(buffer, salloc_header_size);
result = result && (ptr != NULL);
uint32_t blk_size[] = {salloc_header_size + salloc_header_size + salloc_header_size - 1};
bool blk_free[] = {false};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
return result;
void *ptr;
bool result = true;
lb_salloc_init(buffer, salloc_header_size + salloc_header_size + salloc_header_size - 1);
ptr = lb_salloc(buffer, salloc_header_size);
result = result && (ptr != NULL);
uint32_t blk_size[] = {salloc_header_size + salloc_header_size + salloc_header_size - 1};
bool blk_free[] = {false};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
return result;
}
static bool salloc_basic_free(void)
{
void* ptr;
bool result = true;
lb_salloc_init(buffer, 1024);
ptr = lb_salloc(buffer, 10);
result = result && (ptr != NULL);
void *ptr;
bool result = true;
lb_salloc_init(buffer, 1024);
ptr = lb_salloc(buffer, 10);
result = result && (ptr != NULL);
lb_sfree(buffer, ptr);
uint32_t blk_size[] = {1024};
bool blk_free[] = {true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
return result;
lb_sfree(buffer, ptr);
uint32_t blk_size[] = {1024};
bool blk_free[] = {true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
return result;
}
static bool salloc_full_free(void)
{
void* ptr;
bool result = true;
lb_salloc_init(buffer, 1024);
ptr = lb_salloc(buffer, 1024 - salloc_header_size);
result = result && (ptr != NULL);
void *ptr;
bool result = true;
lb_salloc_init(buffer, 1024);
ptr = lb_salloc(buffer, 1024 - salloc_header_size);
result = result && (ptr != NULL);
lb_sfree(buffer, ptr);
uint32_t blk_size[] = {1024};
bool blk_free[] = {true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
return result;
lb_sfree(buffer, ptr);
uint32_t blk_size[] = {1024};
bool blk_free[] = {true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
return result;
}
static bool salloc_multiple_free(void)
{
void* ptr1, *ptr2, *ptr3, *ptr4;
bool result = true;
lb_salloc_init(buffer, 1024);
ptr1 = lb_salloc(buffer, 10);
ptr2 = lb_salloc(buffer, 10);
ptr3 = lb_salloc(buffer, 10);
ptr4 = lb_salloc(buffer, 10);
result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL);
lb_sfree(buffer, ptr1);
lb_sfree(buffer, ptr3);
void *ptr1, *ptr2, *ptr3, *ptr4;
bool result = true;
lb_salloc_init(buffer, 1024);
ptr1 = lb_salloc(buffer, 10);
ptr2 = lb_salloc(buffer, 10);
ptr3 = lb_salloc(buffer, 10);
ptr4 = lb_salloc(buffer, 10);
result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL);
lb_sfree(buffer, ptr1);
lb_sfree(buffer, ptr3);
uint32_t blk_size[] = {10 + salloc_header_size,
10 + salloc_header_size,
10 + salloc_header_size,
10 + salloc_header_size,
1024-4*(10+salloc_header_size)};
bool blk_free[] = {true,false,true,false,true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 5);
return result;
uint32_t blk_size[] = {10 + salloc_header_size,
10 + salloc_header_size,
10 + salloc_header_size,
10 + salloc_header_size,
1024 - 4 * (10 + salloc_header_size)};
bool blk_free[] = {true, false, true, false, true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 5);
return result;
}
static bool salloc_free_join_tail(void)
{
void* ptr1, *ptr2, *ptr3, *ptr4;
bool result = true;
lb_salloc_init(buffer, 1024);
ptr1 = lb_salloc(buffer, 10);
ptr2 = lb_salloc(buffer, 10);
ptr3 = lb_salloc(buffer, 10);
ptr4 = lb_salloc(buffer, 10);
result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL);
lb_sfree(buffer, ptr4);
void *ptr1, *ptr2, *ptr3, *ptr4;
bool result = true;
lb_salloc_init(buffer, 1024);
ptr1 = lb_salloc(buffer, 10);
ptr2 = lb_salloc(buffer, 10);
ptr3 = lb_salloc(buffer, 10);
ptr4 = lb_salloc(buffer, 10);
result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL);
lb_sfree(buffer, ptr4);
uint32_t blk_size[] = {10 + salloc_header_size,
10 + salloc_header_size,
10 + salloc_header_size,
1024-3*(10+salloc_header_size)};
bool blk_free[] = {false,false,false,true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 4);
return result;
uint32_t blk_size[] = {10 + salloc_header_size,
10 + salloc_header_size,
10 + salloc_header_size,
1024 - 3 * (10 + salloc_header_size)};
bool blk_free[] = {false, false, false, true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 4);
return result;
}
static bool salloc_free_join_head(void)
{
void* ptr1, *ptr2, *ptr3, *ptr4;
bool result = true;
lb_salloc_init(buffer, 1024);
ptr1 = lb_salloc(buffer, 10);
ptr2 = lb_salloc(buffer, 10);
ptr3 = lb_salloc(buffer, 10);
ptr4 = lb_salloc(buffer, 10);
result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL);
lb_sfree(buffer, ptr1);
lb_sfree(buffer, ptr2);
void *ptr1, *ptr2, *ptr3, *ptr4;
bool result = true;
lb_salloc_init(buffer, 1024);
ptr1 = lb_salloc(buffer, 10);
ptr2 = lb_salloc(buffer, 10);
ptr3 = lb_salloc(buffer, 10);
ptr4 = lb_salloc(buffer, 10);
result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL);
lb_sfree(buffer, ptr1);
lb_sfree(buffer, ptr2);
uint32_t blk_size[] = {2*(10 + salloc_header_size),
10 + salloc_header_size,
10 + salloc_header_size,
1024-4*(10+salloc_header_size)};
bool blk_free[] = {true,false,false,true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 4);
return result;
uint32_t blk_size[] = {2 * (10 + salloc_header_size),
10 + salloc_header_size,
10 + salloc_header_size,
1024 - 4 * (10 + salloc_header_size)};
bool blk_free[] = {true, false, false, true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 4);
return result;
}
static bool salloc_free_join_mid(void)
{
void* ptr1, *ptr2, *ptr3, *ptr4;
bool result = true;
lb_salloc_init(buffer, 1024);
ptr1 = lb_salloc(buffer, 10);
ptr2 = lb_salloc(buffer, 10);
ptr3 = lb_salloc(buffer, 10);
ptr4 = lb_salloc(buffer, 10);
result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL);
lb_sfree(buffer, ptr2);
lb_sfree(buffer, ptr3);
void *ptr1, *ptr2, *ptr3, *ptr4;
bool result = true;
lb_salloc_init(buffer, 1024);
ptr1 = lb_salloc(buffer, 10);
ptr2 = lb_salloc(buffer, 10);
ptr3 = lb_salloc(buffer, 10);
ptr4 = lb_salloc(buffer, 10);
result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL);
lb_sfree(buffer, ptr2);
lb_sfree(buffer, ptr3);
uint32_t blk_size[] = {10 + salloc_header_size,
2*(10 + salloc_header_size),
10 + salloc_header_size,
1024-4*(10+salloc_header_size)};
bool blk_free[] = {false,true,false,true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 4);
return result;
uint32_t blk_size[] = {10 + salloc_header_size,
2 * (10 + salloc_header_size),
10 + salloc_header_size,
1024 - 4 * (10 + salloc_header_size)};
bool blk_free[] = {false, true, false, true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 4);
return result;
}
static bool salloc_free_join_consecutive(void)
{
void* ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
bool result = true;
lb_salloc_init(buffer, 1024);
ptr1 = lb_salloc(buffer, 10);
ptr2 = lb_salloc(buffer, 10);
ptr3 = lb_salloc(buffer, 10);
ptr4 = lb_salloc(buffer, 10);
ptr5 = lb_salloc(buffer, 10);
result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL) && (ptr5 != NULL);
lb_sfree(buffer, ptr2);
lb_sfree(buffer, ptr4);
void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
bool result = true;
lb_salloc_init(buffer, 1024);
ptr1 = lb_salloc(buffer, 10);
ptr2 = lb_salloc(buffer, 10);
ptr3 = lb_salloc(buffer, 10);
ptr4 = lb_salloc(buffer, 10);
ptr5 = lb_salloc(buffer, 10);
result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL) && (ptr5 != NULL);
lb_sfree(buffer, ptr2);
lb_sfree(buffer, ptr4);
uint32_t blk_size[] = {10 + salloc_header_size,
10 + salloc_header_size,
10 + salloc_header_size,
10 + salloc_header_size,
10 + salloc_header_size,
1024-5*(10+salloc_header_size)};
bool blk_free[] = {false,true,false,true,false,true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 6);
uint32_t blk_size[] = {10 + salloc_header_size,
10 + salloc_header_size,
10 + salloc_header_size,
10 + salloc_header_size,
10 + salloc_header_size,
1024 - 5 * (10 + salloc_header_size)};
bool blk_free[] = {false, true, false, true, false, true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 6);
lb_sfree(buffer, ptr3);
lb_sfree(buffer, ptr3);
uint32_t blk_size2[] = {10 + salloc_header_size,
3*(10 + salloc_header_size),
10 + salloc_header_size,
1024-5*(10+salloc_header_size)};
bool blk_free2[] = {false,true,false,true};
result = result && lb_salloc_assert(buffer, blk_size2, blk_free2, 4);
return result;
uint32_t blk_size2[] = {10 + salloc_header_size,
3 * (10 + salloc_header_size),
10 + salloc_header_size,
1024 - 5 * (10 + salloc_header_size)};
bool blk_free2[] = {false, true, false, true};
result = result && lb_salloc_assert(buffer, blk_size2, blk_free2, 4);
return result;
}
static bool salloc_free_all(void)
{
void* ptr1, *ptr2, *ptr3, *ptr4;
bool result = true;
lb_salloc_init(buffer, 1024);
ptr1 = lb_salloc(buffer, 10);
ptr2 = lb_salloc(buffer, 10);
ptr3 = lb_salloc(buffer, 10);
ptr4 = lb_salloc(buffer, 10);
result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL);
lb_sfree(buffer, ptr1);
lb_sfree(buffer, ptr2);
lb_sfree(buffer, ptr3);
lb_sfree(buffer, ptr4);
void *ptr1, *ptr2, *ptr3, *ptr4;
bool result = true;
lb_salloc_init(buffer, 1024);
ptr1 = lb_salloc(buffer, 10);
ptr2 = lb_salloc(buffer, 10);
ptr3 = lb_salloc(buffer, 10);
ptr4 = lb_salloc(buffer, 10);
result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL);
lb_sfree(buffer, ptr1);
lb_sfree(buffer, ptr2);
lb_sfree(buffer, ptr3);
lb_sfree(buffer, ptr4);
uint32_t blk_size[] = {1024};
bool blk_free[] = {true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
return result;
uint32_t blk_size[] = {1024};
bool blk_free[] = {true};
result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1);
return result;
}
void KABI salloc_test(void)
{
test_begin("salloc test");
test_begin("salloc test");
run_case("salloc_init_test", salloc_init_test());
run_case("salloc_init_test", salloc_init_test());
run_case("salloc_basic_alloc", salloc_basic_alloc());
run_case("salloc_full_alloc", salloc_full_alloc());
run_case("salloc_overflow_alloc", salloc_overflow_alloc());
run_case("salloc_multiple_alloc", salloc_multiple_alloc());
run_case("salloc_alloc_not_enough", salloc_alloc_not_enough());
run_case("salloc_basic_alloc", salloc_basic_alloc());
run_case("salloc_full_alloc", salloc_full_alloc());
run_case("salloc_overflow_alloc", salloc_overflow_alloc());
run_case("salloc_multiple_alloc", salloc_multiple_alloc());
run_case("salloc_alloc_not_enough", salloc_alloc_not_enough());
run_case("salloc_basic_free", salloc_basic_free());
run_case("salloc_full_free", salloc_full_free());
run_case("salloc_multiple_free", salloc_multiple_free());
run_case("salloc_free_join_tail", salloc_free_join_tail());
run_case("salloc_free_join_head", salloc_free_join_head());
run_case("salloc_free_join_mid", salloc_free_join_mid());
run_case("salloc_free_join_consecutive", salloc_free_join_consecutive());
run_case("salloc_free_all", salloc_free_all());
run_case("salloc_basic_free", salloc_basic_free());
run_case("salloc_full_free", salloc_full_free());
run_case("salloc_multiple_free", salloc_multiple_free());
run_case("salloc_free_join_tail", salloc_free_join_tail());
run_case("salloc_free_join_head", salloc_free_join_head());
run_case("salloc_free_join_mid", salloc_free_join_mid());
run_case("salloc_free_join_consecutive", salloc_free_join_consecutive());
run_case("salloc_free_all", salloc_free_all());
test_end();
test_end();
}