[SeaBIOS] [PATCH] Introduce standard warnings for allocation failures and timeouts.

Kevin O'Connor kevin at koconnor.net
Mon Feb 15 04:05:39 CET 2010


    Introduce standard warnings for allocation failures and timeouts.

    There is no need for custom warnings for many common failures.
    Introduce a common warning which is consistent and more visible.

diff --git a/src/acpi.c b/src/acpi.c
index 651f3a2..0559443 100644
--- a/src/acpi.c
+++ b/src/acpi.c
@@ -303,7 +303,7 @@ build_fadt(int bdf)
     void *dsdt = malloc_high(sizeof(AmlCode));
 
     if (!fadt || !facs || !dsdt) {
-        dprintf(1, "Not enough memory for fadt!\n");
+        warn_noalloc();
         return NULL;
     }
 
@@ -353,7 +353,7 @@ build_madt(void)
                      + sizeof(struct madt_intsrcovr) * 16);
     struct multiple_apic_table *madt = malloc_high(madt_size);
     if (!madt) {
-        dprintf(1, "Not enough memory for madt!\n");
+        warn_noalloc();
         return NULL;
     }
     memset(madt, 0, madt_size);
@@ -418,7 +418,7 @@ build_ssdt(void)
     int length = sizeof(struct acpi_table_header) + 3 + cpu_length;
     u8 *ssdt = malloc_high(length);
     if (! ssdt) {
-        dprintf(1, "No space for ssdt!\n");
+        warn_noalloc();
         return NULL;
     }
 
@@ -473,7 +473,7 @@ build_hpet(void)
 {
     struct acpi_20_hpet *hpet = malloc_high(sizeof(*hpet));
     if (!hpet) {
-        dprintf(1, "Not enough memory for hpet!\n");
+        warn_noalloc();
         return NULL;
     }
 
@@ -514,7 +514,7 @@ build_srat(void)
 
     u64 *numadata = malloc_tmphigh(sizeof(u64) * (MaxCountCPUs + nb_numa_nodes));
     if (!numadata) {
-        dprintf(1, "Not enough memory for read numa data from VM!\n");
+        warn_noalloc();
         return NULL;
     }
 
@@ -527,7 +527,7 @@ build_srat(void)
 
     srat = malloc_high(srat_size);
     if (!srat) {
-        dprintf(1, "Not enough memory for srat table!\n");
+        warn_noalloc();
         free(numadata);
         return NULL;
     }
@@ -620,7 +620,7 @@ acpi_bios_init(void)
     // Create initial rsdt table
     struct rsdp_descriptor *rsdp = malloc_fseg(sizeof(*rsdp));
     if (!rsdp) {
-        dprintf(1, "Not enough memory for acpi rsdp table!\n");
+        warn_noalloc();
         return;
     }
 
@@ -646,13 +646,12 @@ acpi_bios_init(void)
         u16 len = qemu_cfg_next_acpi_table_len();
         void *addr = malloc_high(len);
         if (!addr) {
-            dprintf(1, "Not enough memory for ext acpi table of size %d!\n"
-                    , len);
+            warn_noalloc();
             continue;
         }
         ACPI_INIT_TABLE(qemu_cfg_next_acpi_table_load(addr, len));
         if (tbl_idx == MAX_ACPI_TABLES) {
-            dprintf(1, "Too many external tables!\n");
+            warn_noalloc();
             break;
         }
     }
@@ -662,7 +661,7 @@ acpi_bios_init(void)
     rsdt = malloc_high(rsdt_len);
 
     if (!rsdt) {
-        dprintf(1, "Not enough memory for acpi rsdt table!\n");
+        warn_noalloc();
         return;
     }
     memset(rsdt, 0, rsdt_len);
diff --git a/src/ata.c b/src/ata.c
index ceb08f2..ed7ef28 100644
--- a/src/ata.c
+++ b/src/ata.c
@@ -37,7 +37,7 @@ await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
         if ((status & mask) == flags)
             return status;
         if (check_time(end)) {
-            dprintf(1, "IDE time out\n");
+            warn_timeout();
             return -1;
         }
         yield();
@@ -109,7 +109,7 @@ ata_reset(struct drive_s *drive_g)
                 break;
             // Change drive request failed to take effect - retry.
             if (check_time(end)) {
-                dprintf(1, "ata_reset slave time out\n");
+                warn_timeout();
                 goto done;
             }
         }
@@ -458,7 +458,7 @@ ata_dma_transfer(struct disk_op_s *op)
         // Transfer in progress
         if (check_time(end)) {
             // Timeout.
-            dprintf(1, "IDE DMA timeout\n");
+            warn_timeout();
             break;
         }
         yield();
@@ -883,7 +883,7 @@ powerup_await_non_bsy(u16 base)
             return orstatus;
         }
         if (check_time(SpinupEnd)) {
-            dprintf(1, "powerup IDE time out\n");
+            warn_timeout();
             return -1;
         }
         yield();
diff --git a/src/block.c b/src/block.c
index 01aa84a..3a9a68d 100644
--- a/src/block.c
+++ b/src/block.c
@@ -224,7 +224,7 @@ static void
 add_ordered_drive(u8 *idmap, u8 *count, struct drive_s *drive_g)
 {
     if (*count >= ARRAY_SIZE(Drives.idmap[0])) {
-        dprintf(1, "No room to map drive %p\n", drive_g);
+        warn_noalloc();
         return;
     }
     u8 *pos = &idmap[*count];
diff --git a/src/bregs.h b/src/bregs.h
index 0cdd016..9a381d0 100644
--- a/src/bregs.h
+++ b/src/bregs.h
@@ -91,29 +91,6 @@ set_code_invalid_silent(struct bregs *regs, u8 code)
     set_cf(regs, 1);
 }
 
-#define warn_invalid(regs)                      \
-    __warn_invalid((regs), __LINE__, __func__)
-#define set_invalid(regs)                       \
-    __set_invalid((regs), __LINE__, __func__)
-#define set_code_invalid(regs, code)                                    \
-    __set_code_invalid((regs), (code) | (__LINE__ << 8), __func__)
-
-#define warn_unimplemented(regs)                        \
-    __warn_unimplemented((regs), __LINE__, __func__)
-#define set_unimplemented(regs)                         \
-    __set_unimplemented((regs), __LINE__, __func__)
-#define set_code_unimplemented(regs, code)                              \
-    __set_code_unimplemented((regs), (code) | (__LINE__ << 8), __func__)
-
-// output.c
-void __warn_invalid(struct bregs *regs, int lineno, const char *fname);
-void __warn_unimplemented(struct bregs *regs, int lineno, const char *fname);
-void __set_invalid(struct bregs *regs, int lineno, const char *fname);
-void __set_unimplemented(struct bregs *regs, int lineno, const char *fname);
-void __set_code_invalid(struct bregs *regs, u32 linecode, const char *fname);
-void __set_code_unimplemented(struct bregs *regs, u32 linecode
-                              , const char *fname);
-
 #endif // !__ASSEMBLY__
 
 #endif // bregs.h
diff --git a/src/coreboot.c b/src/coreboot.c
index 7ad46ce..9887752 100644
--- a/src/coreboot.c
+++ b/src/coreboot.c
@@ -209,7 +209,7 @@ copy_pir(void *pos)
         return;
     void *newpos = malloc_fseg(p->size);
     if (!newpos) {
-        dprintf(1, "No room to copy PIR table!\n");
+        warn_noalloc();
         return;
     }
     dprintf(1, "Copying PIR from %p to %p\n", pos, newpos);
@@ -231,7 +231,7 @@ copy_mptable(void *pos)
     u16 mpclength = ((struct mptable_config_s *)p->physaddr)->length;
     struct mptable_floating_s *newpos = malloc_fseg(length + mpclength);
     if (!newpos) {
-        dprintf(1, "No room to copy MPTABLE!\n");
+        warn_noalloc();
         return;
     }
     dprintf(1, "Copying MPTABLE from %p/%x to %p\n", pos, p->physaddr, newpos);
@@ -259,7 +259,7 @@ copy_acpi_rsdp(void *pos)
     }
     void *newpos = malloc_fseg(length);
     if (!newpos) {
-        dprintf(1, "No room to copy ACPI RSDP table!\n");
+        warn_noalloc();
         return;
     }
     dprintf(1, "Copying ACPI RSDP from %p to %p\n", pos, newpos);
@@ -375,8 +375,8 @@ cbfs_setup(void)
 
     CBHDR = *(void **)CBFS_HEADPTR_ADDR;
     if (CBHDR->magic != htonl(CBFS_HEADER_MAGIC)) {
-        dprintf(1, "Unable to find CBFS (got %x not %x)\n"
-                , CBHDR->magic, htonl(CBFS_HEADER_MAGIC));
+        dprintf(1, "Unable to find CBFS (ptr=%p; got %x not %x)\n"
+                , CBHDR, CBHDR->magic, htonl(CBFS_HEADER_MAGIC));
         CBHDR = NULL;
         return;
     }
@@ -524,7 +524,7 @@ cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen)
     // Not compressed.
     dprintf(3, "Copying data %d@%p to %d@%p\n", size, src, maxlen, dst);
     if (size > maxlen) {
-        dprintf(1, "File too big to copy\n");
+        warn_noalloc();
         return -1;
     }
     iomemcpy(dst, src, size);
diff --git a/src/disk.c b/src/disk.c
index 4457ea9..eb68e87 100644
--- a/src/disk.c
+++ b/src/disk.c
@@ -88,8 +88,7 @@ basic_access(struct bregs *regs, struct drive_s *drive_g, u16 command)
     u16 head = regs->dh;
 
     if (count > 128 || count == 0 || sector == 0) {
-        dprintf(1, "int13_harddisk: function %02x, parameter out of range!\n"
-                , regs->ah);
+        warn_invalid(regs);
         disk_ret(regs, DISK_RET_EPARAM);
         return;
     }
@@ -100,9 +99,7 @@ basic_access(struct bregs *regs, struct drive_s *drive_g, u16 command)
 
     // sanity check on cyl heads, sec
     if (cylinder >= nlc || head >= nlh || sector > nlspt) {
-        dprintf(1, "int13_harddisk: function %02x, parameters out of"
-                " range %04x/%04x/%04x!\n"
-                , regs->ah, cylinder, head, sector);
+        warn_invalid(regs);
         disk_ret(regs, DISK_RET_EPARAM);
         return;
     }
@@ -130,8 +127,7 @@ extended_access(struct bregs *regs, struct drive_s *drive_g, u16 command)
     dop.command = command;
     dop.drive_g = drive_g;
     if (dop.lba >= GET_GLOBAL(drive_g->sectors)) {
-        dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
-                , regs->ah);
+        warn_invalid(regs);
         disk_ret(regs, DISK_RET_EPARAM);
         return;
     }
diff --git a/src/memmap.c b/src/memmap.c
index aa69503..8770bbc 100644
--- a/src/memmap.c
+++ b/src/memmap.c
@@ -27,7 +27,7 @@ static void
 insert_e820(int i, u64 start, u64 size, u32 type)
 {
     if (e820_count >= CONFIG_MAX_E820) {
-        dprintf(1, "Overflowed e820 list!\n");
+        warn_noalloc();
         return;
     }
 
diff --git a/src/mptable.c b/src/mptable.c
index 0fd4737..0e0e1a0 100644
--- a/src/mptable.c
+++ b/src/mptable.c
@@ -23,7 +23,7 @@ mptable_init(void)
     // Config structure in temp area.
     struct mptable_config_s *config = malloc_tmphigh(32*1024);
     if (!config) {
-        dprintf(1, "No space for temp mptable\n");
+        warn_noalloc();
         return;
     }
     memset(config, 0, sizeof(*config));
@@ -186,7 +186,7 @@ mptable_init(void)
     struct mptable_config_s *finalconfig = malloc_fseg(length);
     struct mptable_floating_s *floating = malloc_fseg(sizeof(*floating));
     if (!finalconfig || !floating) {
-        dprintf(1, "No room for MPTABLE!\n");
+        warn_noalloc();
         free(config);
         free(finalconfig);
         free(floating);
diff --git a/src/optionroms.c b/src/optionroms.c
index ace0c0e..47f5808 100644
--- a/src/optionroms.c
+++ b/src/optionroms.c
@@ -185,7 +185,7 @@ copy_rom(struct rom_header *rom)
     u32 romsize = rom->size * 512;
     if (RomEnd + romsize > max_rom()) {
         // Option rom doesn't fit.
-        dprintf(1, "Option rom %p doesn't fit.\n", rom);
+        warn_noalloc();
         return NULL;
     }
     dprintf(4, "Copying option rom (size %d) from %p to %x\n"
diff --git a/src/output.c b/src/output.c
index 3de565a..ae105fb 100644
--- a/src/output.c
+++ b/src/output.c
@@ -485,6 +485,21 @@ __warn_unimplemented(struct bregs *regs, int lineno, const char *fname)
     }
 }
 
+// Report on an allocation failure.
+void
+__warn_noalloc(int lineno, const char *fname)
+{
+    dprintf(1, "WARNING - Unable to allocate resource at %s:%d!\n"
+            , fname, lineno);
+}
+
+// Report on a timeout exceeded.
+void
+__warn_timeout(int lineno, const char *fname)
+{
+    dprintf(1, "WARNING - Timeout at %s:%d!\n", fname, lineno);
+}
+
 // Report a handler reporting an invalid parameter to the caller.
 void
 __set_invalid(struct bregs *regs, int lineno, const char *fname)
diff --git a/src/paravirt.c b/src/paravirt.c
index 7171bac..fc0cbfe 100644
--- a/src/paravirt.c
+++ b/src/paravirt.c
@@ -211,7 +211,7 @@ int qemu_cfg_smbios_load_external(int type, char **p, unsigned *nr_structs,
         }
 
         if (end - *p < sizeof(struct smbios_structure_header)) {
-            dprintf(1, "No more memory for additional smbios tables\n");
+            warn_noalloc();
             break;
         }
 
@@ -233,7 +233,7 @@ int qemu_cfg_smbios_load_external(int type, char **p, unsigned *nr_structs,
 
         /* Read the rest and terminate the entry */
         if (end - *p < table.header.length) {
-            dprintf(1, "No memory for smbios table %d\n", header->type);
+            warn_noalloc();
             *p -= sizeof(struct smbios_structure_header);
             continue;
         }
diff --git a/src/ps2port.c b/src/ps2port.c
index 4cfff28..49bf551 100644
--- a/src/ps2port.c
+++ b/src/ps2port.c
@@ -32,7 +32,7 @@ i8042_wait_read(void)
             return 0;
         udelay(50);
     }
-    dprintf(1, "i8042 timeout on wait read\n");
+    warn_timeout();
     return -1;
 }
 
@@ -47,7 +47,7 @@ i8042_wait_write(void)
             return 0;
         udelay(50);
     }
-    dprintf(1, "i8042 timeout on wait write\n");
+    warn_timeout();
     return -1;
 }
 
@@ -65,7 +65,7 @@ i8042_flush(void)
         dprintf(7, "i8042 flushed %x (status=%x)\n", data, status);
     }
 
-    dprintf(1, "i8042 timeout on flush\n");
+    warn_timeout();
     return -1;
 }
 
@@ -189,7 +189,9 @@ ps2_recvbyte(int aux, int needack, int timeout)
         }
 
         if (check_time(end)) {
-            dprintf(1, "ps2_recvbyte timeout\n");
+            // Don't warn on second byte of a reset
+            if (timeout > 100)
+                warn_timeout();
             return -1;
         }
         yield();
diff --git a/src/ramdisk.c b/src/ramdisk.c
index 16c8b25..641d87e 100644
--- a/src/ramdisk.c
+++ b/src/ramdisk.c
@@ -37,7 +37,7 @@ ramdisk_setup(void)
     // Allocate ram for image.
     void *pos = memalign_tmphigh(PAGE_SIZE, size);
     if (!pos) {
-        dprintf(3, "Not enough memory for ramdisk\n");
+        warn_noalloc();
         return;
     }
     add_e820((u32)pos, size, E820_RESERVED);
diff --git a/src/smbios.c b/src/smbios.c
index 870afbe..f948d78 100644
--- a/src/smbios.c
+++ b/src/smbios.c
@@ -19,7 +19,7 @@ smbios_entry_point_init(u16 max_structure_size,
     struct smbios_entry_point *ep = malloc_fseg(sizeof(*ep));
     void *finaltable = malloc_high(structure_table_length);
     if (!ep || !finaltable) {
-        dprintf(1, "No space for smbios tables!\n");
+        warn_noalloc();
         free(ep);
         free(finaltable);
         return;
@@ -378,7 +378,7 @@ smbios_init(void)
 
     char *start = malloc_tmphigh(TEMPSMBIOSSIZE);
     if (! start) {
-        dprintf(1, "No memory for temp smbios table\n");
+        warn_noalloc();
         return;
     }
 
diff --git a/src/usb-ohci.c b/src/usb-ohci.c
index 8ebd340..421c19b 100644
--- a/src/usb-ohci.c
+++ b/src/usb-ohci.c
@@ -35,7 +35,7 @@ start_ohci(struct usb_s *cntl, struct ohci_hcca *hcca)
         if (! status & OHCI_HCR)
             break;
         if (check_time(end)) {
-            dprintf(1, "Timeout on ohci software reset\n");
+            warn_timeout();
             return -1;
         }
     }
@@ -153,7 +153,7 @@ ohci_init(void *data)
     struct ohci_ed *intr_ed = malloc_high(sizeof(*intr_ed));
     struct ohci_ed *control_ed = malloc_high(sizeof(*control_ed));
     if (!hcca || !intr_ed || !control_ed) {
-        dprintf(1, "No ram for ohci init\n");
+        warn_noalloc();
         goto free;
     }
     memset(hcca, 0, sizeof(*hcca));
@@ -192,7 +192,7 @@ wait_ed(struct ohci_ed *ed)
         if (ed->hwHeadP == ed->hwTailP)
             return 0;
         if (check_time(end)) {
-            dprintf(1, "Timeout on wait_ed %p\n", ed);
+            warn_timeout();
             return -1;
         }
         yield();
diff --git a/src/usb-uhci.c b/src/usb-uhci.c
index b09b17a..b7ff394 100644
--- a/src/usb-uhci.c
+++ b/src/usb-uhci.c
@@ -40,12 +40,12 @@ configure_uhci(struct usb_s *cntl)
     struct uhci_qh *data_qh = malloc_high(sizeof(*data_qh));
     struct uhci_qh *term_qh = malloc_high(sizeof(*term_qh));
     if (!term_td || !fl || !intr_qh || !data_qh || !term_qh) {
+        warn_noalloc();
         free(term_td);
         free(fl);
         free(intr_qh);
         free(data_qh);
         free(term_qh);
-        dprintf(1, "No ram for uhci init\n");
         return;
     }
 
@@ -169,6 +169,7 @@ wait_qh(struct usb_s *cntl, struct uhci_qh *qh)
         if (qh->element & UHCI_PTR_TERM)
             return 0;
         if (check_time(end)) {
+            warn_timeout();
             struct uhci_td *td = (void*)(qh->element & ~UHCI_PTR_BITS);
             dprintf(1, "Timeout on wait_qh %p (td=%p s=%x c=%x/%x)\n"
                     , qh, td, td->status
diff --git a/src/util.h b/src/util.h
index 429590c..4228904 100644
--- a/src/util.h
+++ b/src/util.h
@@ -203,17 +203,28 @@ void panic(const char *fmt, ...)
     __attribute__ ((format (printf, 1, 2))) __noreturn;
 void printf(const char *fmt, ...)
     __attribute__ ((format (printf, 1, 2)));
-void __dprintf(const char *fmt, ...)
-    __attribute__ ((format (printf, 1, 2)));
 int snprintf(char *str, size_t size, const char *fmt, ...)
     __attribute__ ((format (printf, 3, 4)));
+void __dprintf(const char *fmt, ...)
+    __attribute__ ((format (printf, 1, 2)));
+void __debug_enter(struct bregs *regs, const char *fname);
+void __debug_isr(const char *fname);
+void __debug_stub(struct bregs *regs, int lineno, const char *fname);
+void __warn_invalid(struct bregs *regs, int lineno, const char *fname);
+void __warn_unimplemented(struct bregs *regs, int lineno, const char *fname);
+void __warn_noalloc(int lineno, const char *fname);
+void __warn_timeout(int lineno, const char *fname);
+void __set_invalid(struct bregs *regs, int lineno, const char *fname);
+void __set_unimplemented(struct bregs *regs, int lineno, const char *fname);
+void __set_code_invalid(struct bregs *regs, u32 linecode, const char *fname);
+void __set_code_unimplemented(struct bregs *regs, u32 linecode
+                              , const char *fname);
+void hexdump(const void *d, int len);
+
 #define dprintf(lvl, fmt, args...) do {                         \
         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
             __dprintf((fmt) , ##args );                         \
     } while (0)
-void __debug_enter(struct bregs *regs, const char *fname);
-void __debug_stub(struct bregs *regs, int lineno, const char *fname);
-void __debug_isr(const char *fname);
 #define debug_enter(regs, lvl) do {                     \
         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
             __debug_enter((regs), __func__);            \
@@ -224,7 +235,22 @@ void __debug_isr(const char *fname);
     } while (0)
 #define debug_stub(regs)                        \
     __debug_stub((regs), __LINE__, __func__)
-void hexdump(const void *d, int len);
+#define warn_invalid(regs)                      \
+    __warn_invalid((regs), __LINE__, __func__)
+#define warn_unimplemented(regs)                        \
+    __warn_unimplemented((regs), __LINE__, __func__)
+#define warn_noalloc()                          \
+    __warn_noalloc(__LINE__, __func__)
+#define warn_timeout()                          \
+    __warn_timeout(__LINE__, __func__)
+#define set_invalid(regs)                       \
+    __set_invalid((regs), __LINE__, __func__)
+#define set_code_invalid(regs, code)                                    \
+    __set_code_invalid((regs), (code) | (__LINE__ << 8), __func__)
+#define set_unimplemented(regs)                         \
+    __set_unimplemented((regs), __LINE__, __func__)
+#define set_code_unimplemented(regs, code)                              \
+    __set_code_unimplemented((regs), (code) | (__LINE__ << 8), __func__)
 
 // kbd.c
 void kbd_setup(void);



More information about the SeaBIOS mailing list