+
+#if CONFIG_SERIAL_KDP
+
+static boolean_t needs_serial_init = TRUE;
+
+static void
+kdp_serial_send(void *rpkt, unsigned int rpkt_len)
+{
+ // printf("tx\n");
+ kdp_serialize_packet((unsigned char *)rpkt, rpkt_len, pal_serial_putc);
+}
+
+static void
+kdp_serial_receive(void *rpkt, unsigned int *rpkt_len, unsigned int timeout)
+{
+ int readkar;
+ uint64_t now, deadline;
+
+ clock_interval_to_deadline(timeout, 1000 * 1000 /* milliseconds */, &deadline);
+
+// printf("rx\n");
+ for(clock_get_uptime(&now); now < deadline; clock_get_uptime(&now))
+ {
+ readkar = pal_serial_getc();
+ if(readkar >= 0)
+ {
+ unsigned char *packet;
+ // printf("got char %02x\n", readkar);
+ if((packet = kdp_unserialize_packet(readkar,rpkt_len)))
+ {
+ memcpy(rpkt, packet, *rpkt_len);
+ return;
+ }
+ }
+ }
+ *rpkt_len = 0;
+}
+
+static boolean_t
+kdp_serial_setmode(boolean_t active)
+{
+ if (active == FALSE) /* leaving KDP */
+ return TRUE;
+
+ if (!needs_serial_init)
+ return TRUE;
+
+ pal_serial_init();
+ needs_serial_init = FALSE;
+ return TRUE;
+}
+
+
+static void kdp_serial_callout(__unused void *arg, kdp_event_t event)
+{
+ /* When we stop KDP, set the bit to re-initialize the console serial port
+ * the next time we send/receive a KDP packet. We don't do it on
+ * KDP_EVENT_ENTER directly because it also gets called when we trap to KDP
+ * for non-external debugging, i.e., stackshot or core dumps.
+ *
+ * Set needs_serial_init on exit (and initialization, see above) and not
+ * enter because enter is sent multiple times and causes excess reinitialization.
+ */
+
+ switch (event)
+ {
+ case KDP_EVENT_PANICLOG:
+ case KDP_EVENT_ENTER:
+ break;
+ case KDP_EVENT_EXIT:
+ needs_serial_init = TRUE;
+ break;
+ }
+}
+
+#endif /* CONFIG_SERIAL_KDP */
+
+void
+kdp_init(void)
+{
+ strlcpy(kdp_kernelversion_string, version, sizeof(kdp_kernelversion_string));
+
+ /* Relies on platform layer calling panic_init() before kdp_init() */
+ if (kernel_uuid_string[0] != '\0') {
+ /*
+ * Update kdp_kernelversion_string with our UUID
+ * generated at link time.
+ */
+
+ strlcat(kdp_kernelversion_string, "; UUID=", sizeof(kdp_kernelversion_string));
+ strlcat(kdp_kernelversion_string, kernel_uuid_string, sizeof(kdp_kernelversion_string));
+ }
+
+ debug_log_init();
+
+#if defined(__x86_64__) || defined(__arm__)
+ if (vm_kernel_slide) {
+ char KASLR_stext[19];
+ strlcat(kdp_kernelversion_string, "; stext=", sizeof(kdp_kernelversion_string));
+ snprintf(KASLR_stext, sizeof(KASLR_stext), "%p", (void *) vm_kernel_stext);
+ strlcat(kdp_kernelversion_string, KASLR_stext, sizeof(kdp_kernelversion_string));
+ }
+#endif
+
+ if (debug_boot_arg & DB_REBOOT_POST_CORE)
+ kdp_flag |= REBOOT_POST_CORE;
+#if defined(__x86_64__)
+ kdp_machine_init();
+#endif
+
+ kdp_timer_callout_init();
+ kdp_crashdump_feature_mask = htonl(kdp_crashdump_feature_mask);
+
+#if CONFIG_SERIAL_KDP
+ char kdpname[80];
+ struct kdp_in_addr ipaddr;
+ struct kdp_ether_addr macaddr;
+
+
+ // serial must be explicitly requested
+ if(!PE_parse_boot_argn("kdp_match_name", kdpname, sizeof(kdpname)) || strncmp(kdpname, "serial", sizeof(kdpname)) != 0)
+ return;
+
+ kprintf("Initializing serial KDP\n");
+
+ kdp_register_callout(kdp_serial_callout, NULL);
+ kdp_register_link(NULL, kdp_serial_setmode);
+ kdp_register_send_receive(kdp_serial_send, kdp_serial_receive);
+
+ /* fake up an ip and mac for early serial debugging */
+ macaddr.ether_addr_octet[0] = 's';
+ macaddr.ether_addr_octet[1] = 'e';
+ macaddr.ether_addr_octet[2] = 'r';
+ macaddr.ether_addr_octet[3] = 'i';
+ macaddr.ether_addr_octet[4] = 'a';
+ macaddr.ether_addr_octet[5] = 'l';
+ ipaddr.s_addr = KDP_SERIAL_IPADDR;
+ kdp_set_ip_and_mac_addresses(&ipaddr, &macaddr);
+
+#endif /* CONFIG_SERIAL_KDP */
+}