Signed-off-by: Daniel P. Smith <dpsm...@apertussolutions.com>
---
 xen/common/domain-builder/fdt.c | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)

diff --git a/xen/common/domain-builder/fdt.c b/xen/common/domain-builder/fdt.c
index 1b3492571b15..414bbf5d9fb1 100644
--- a/xen/common/domain-builder/fdt.c
+++ b/xen/common/domain-builder/fdt.c
@@ -16,6 +16,21 @@
 
 #include "fdt.h"
 
+#define MAX_DOMID  DOMID_FIRST_RESERVED
+static __initdata DECLARE_BITMAP(domid_alloc, MAX_DOMID);
+
+static domid_t __init find_next_domid(void)
+{
+    unsigned long n = find_next_zero_bit(domid_alloc, MAX_DOMID, 1);
+
+    if ( n == MAX_DOMID )
+        return DOMID_INVALID;
+
+    set_bit(n, domid_alloc);
+
+    return (domid_t) n;
+}
+
 static int __init fdt_prop_as_u32(const struct fdt_property *prop,
                                   uint32_t *val)
 {
@@ -231,18 +246,17 @@ static int __init fdt_process_domain_node(
 
             if ( val >= DOMID_FIRST_RESERVED )
             {
-                printk(XENLOG_ERR "  invalid domain id for domain %s\n", name);
-                return -EINVAL;
-            }
-
-            for ( unsigned int i = 0; i < bi->nr_domains; i++ )
-            {
-                if ( bi->domains[i].domid == val )
+                if ( (val = find_next_domid()) == DOMID_INVALID )
                 {
-                    printk(XENLOG_ERR "  duplicate id for domain %s\n", name);
-                    return -EINVAL;
+                    printk("  unable to allocate domid for domain %s\n", name);
+                    return -EFAULT;
                 }
             }
+            else if ( test_and_set_bit(val, domid_alloc) )
+            {
+                printk(XENLOG_ERR "  duplicate id for domain %s\n", name);
+                return -EINVAL;
+            }
 
             bd->domid = val;
             printk(XENLOG_INFO "  domid: %d\n", bd->domid);
-- 
2.30.2


Reply via email to