#include <amd64/apic.h>
#include <amd64/debug.h>
#include <amd64/gdt.h>
#include <amd64/hpet.h>
#include <amd64/intr.h>
#include <amd64/intr_defs.h>
#include <amd64/msr-index.h>
#include <amd64/msr.h>
#include <amd64/sse.h>
#include <aux/compiler.h>
#include <device/device.h>
#include <device/partitions.h>
#include <devices.h>
#include <fs/vfs.h>
#include <fs_types.h>
#include <irq/irq.h>
#include <libk/std.h>
#include <limine/limine.h>
#include <limine/requests.h>
#include <mm/liballoc.h>
#include <mm/pmm.h>
#include <proc/proc.h>
#include <proc/reschedule.h>
#include <sys/debug.h>
#include <sys/mm.h>
#include <sys/smp.h>
#include <sys/spin.h>
#include <sys/spin_lock.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <uacpi/uacpi.h>

#define UACPI_MEMORY_BUFFER_MAX 4096

ALIGNED (16) static uint8_t uacpi_memory_buffer[UACPI_MEMORY_BUFFER_MAX];

/*
 * The kernel starts booting here. This is the entry point after Limine hands control. We set up all
 * the necessary platform-dependent subsystems/drivers and jump into the init app.
 */
void bootmain (void) {
  struct limine_mp_response* mp = limine_mp_request.response;

  struct cpu* bsp_cpu = cpu_make (mp->bsp_lapic_id, 0);

  gdt_init (bsp_cpu);
  intr_init ();
  syscall_init ();
  debug_init ();
  pmm_init ();
  mm_init ();

  sse_enable ();

  uacpi_setup_early_table_access ((void*)uacpi_memory_buffer, sizeof (uacpi_memory_buffer));

  ioapic_init ();
  hpet_init ();

  devices_init ();
  vfs_init ();

  struct device* rd0 = device_find ("RD0");
  vfs_create_volume ("RD", FS_TARFS, rd0, false);

  struct device* temp0 = device_find ("TEMP0");
  vfs_create_volume ("TEMP", FS_FAT16, temp0, true);

  proc_pid_alloc_init ();
  procgroup_pgid_alloc_init ();

  smp_init ();

  proc_init ();

  for (;;)
    ;
}
