#include <amd64/fx.h>
#include <amd64/gdt.h>
#include <amd64/intr.h>
#include <amd64/mm.h>
#include <amd64/msr-index.h>
#include <amd64/msr.h>
#include <libk/lengthof.h>
#include <libk/list.h>
#include <libk/string.h>
#include <mm/liballoc.h>
#include <proc/proc.h>
#include <proc/reschedule.h>
#include <status.h>
#include <sys/debug.h>
#include <sys/smp.h>
#include <syscall/syscall.h>
#include <syscall_defs.h>

extern void syscall_entry (void);

uintptr_t syscall_dispatch (void* stack_ptr) {
  load_kernel_cr3 ();
  struct saved_regs* regs = stack_ptr;

  spin_lock (&thiscpu->lock);
  struct proc* caller = thiscpu->proc_current;
  int caller_pid = caller->pid;
  spin_lock (&caller->lock);

  memcpy (&caller->pdata.regs, regs, sizeof (struct saved_regs));

  fx_save (caller->pdata.fx_env);

  spin_unlock (&caller->lock);
  spin_unlock (&thiscpu->lock);

  int syscall_num = regs->rax;
  syscall_handler_func_t func = syscall_find_handler (syscall_num);

  if (func == NULL) {
    return -ST_SYSCALL_NOT_FOUND;
  }

  struct reschedule_ctx rctx;
  memset (&rctx, 0, sizeof (rctx));

  uintptr_t r =
      func (caller, regs, &rctx, regs->rdi, regs->rsi, regs->rdx, regs->r10, regs->r8, regs->r9);

  caller = proc_find_pid (caller_pid);

  if (caller != NULL) {
    spin_lock (&caller->lock);
    caller->pdata.regs.rax = r;
    spin_unlock (&caller->lock);
  }

  bool do_thiscpu = false;
  for (size_t i = 0; i < lengthof (rctx.cpus); i++) {
    if (rctx.cpus[i] != NULL && rctx.cpus[i] != thiscpu)
      cpu_request_sched (rctx.cpus[i]);
    else
      do_thiscpu = true;
  }

  if (do_thiscpu)
    cpu_request_sched (thiscpu);

  return r;
}

void syscall_init (void) {
  wrmsr (MSR_STAR, ((uint64_t)GDT_KCODE << 32) | ((uint64_t)(GDT_KDATA | 0x03) << 48));
  wrmsr (MSR_LSTAR, (uint64_t)&syscall_entry);
  wrmsr (MSR_SYSCALL_MASK, (1ULL << 9));
  wrmsr (MSR_EFER, rdmsr (MSR_EFER) | EFER_SCE);
}
