Skip to contentSkip to conversation

Mini-OS: add some macros for asm statements

PATCH In review Juergen Gross 2 participants 3 messages Jul 19, 2024 – Jul 22, 2024 x86

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Mini-OS: add some macros for asm statements

Instead of having #ifdefs sprinkled around in x86 code, add some macros defining constants for asm statements to address differences between 32- and 64-bit mode.

Modify existing code to use those macros.

While at it convert the assembler parts of run_idle_thread() to a more sane variant.

---
V2:
- addressed comments by Andrew Cooper

---
 arch/x86/sched.c | 34 ++++++++++------------------------
 include/x86/os.h | 18 ++++++++++--------
 2 files changed, 20 insertions(+), 32 deletions(-)
arch/x86/sched.c +10 -24
index dabe6fd6..42805f9f 100644
@@ -60,16 +60,10 @@ void dump_stack(struct thread *thread)
     unsigned long *bottom = (unsigned long *)(thread->stack + STACK_SIZE); 
     unsigned long *pointer = (unsigned long *)thread->sp;
     int count;
-    if(thread == current)
-    {
-#ifdef __i386__    
-        asm("movl %%esp,%0"
-            : "=r"(pointer));
-#else
-        asm("movq %%rsp,%0"
-            : "=r"(pointer));
-#endif
-    }
+
+    if ( thread == current )
+        asm("mov %%"ASM_SP",%0" : "=r"(pointer));
+
     printk("The stack for \"%s\"\n", thread->name);
     for(count = 0; count < 25 && pointer < bottom; count ++)
     {
@@ -119,20 +113,12 @@ struct thread* arch_create_thread(char *name, void (*function)(void *),
 
 void run_idle_thread(void)
 {
-    /* Switch stacks and run the thread */ 
-#if defined(__i386__)
-    __asm__ __volatile__("mov %0,%%esp\n\t"
-                         "push %1\n\t" 
-                         "ret"                                            
-                         :"=m" (idle_thread->sp)
-                         :"m" (idle_thread->ip));                          
-#elif defined(__x86_64__)
-    __asm__ __volatile__("mov %0,%%rsp\n\t"
-                         "push %1\n\t" 
-                         "ret"                                            
-                         :"=m" (idle_thread->sp)
-                         :"m" (idle_thread->ip));                                                    
-#endif
+    /* Switch stacks and run the thread */
+    asm volatile ("mov %[sp], %%"ASM_SP"\n\t"
+                  "jmp *%[ip]\n\t"
+                  :
+                  : [sp] "m" (idle_thread->sp),
+                    [ip] "m" (idle_thread->ip));
 }
 
 unsigned long __local_irq_save(void)
include/x86/os.h +10 -8
index ee34d784..0095be13 100644
@@ -61,6 +61,16 @@
 #define TRAP_deferred_nmi     31
 #define TRAP_xen_callback     32
 
+#if defined(__i386__)
+#define __SZ    "l"
+#define __REG   "e"
+#else
+#define __SZ    "q"
+#define __REG   "r"
+#endif
+
+#define ASM_SP  __REG"sp"
+
 /* Everything below this point is not included by assembler (.S) files. */
 #ifndef __ASSEMBLY__
 
@@ -141,14 +151,6 @@ do {									\
 
 #else
 
-#if defined(__i386__)
-#define __SZ "l"
-#define __REG "e"
-#else
-#define __SZ "q"
-#define __REG "r"
-#endif
-
 #define __cli() asm volatile ( "cli" : : : "memory" )
 #define __sti() asm volatile ( "sti" : : : "memory" )
 
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Juergen Gross
arch/x86/sched.c +0 -3
@@ -60,16 +60,10 @@ void dump_stack(struct thread *thread)
     unsigned long *bottom = (unsigned long *)(thread->stack + STACK_SIZE); 
     unsigned long *pointer = (unsigned long *)thread->sp;
     int count;
-    if(thread == current)
-    {
-#ifdef __i386__    

arch/x86/sched.c void dump_stack(struct thread *thread) · line 65

Quoted 10 lines
arch/x86/sched.c +3 -7
-        asm("movl %%esp,%0"
-            : "=r"(pointer));
-#else
-        asm("movq %%rsp,%0"
-            : "=r"(pointer));
-#endif
-    }
+
+    if ( thread == current )
+        asm("mov %%"ASM_SP",%0" : "=r"(pointer));

As you switch the if() to Xen style, why not also the asm()? Irrespective of which precise style is meant to be used, the last closing double quote likely also wants to be followed by a blank?

Quoted 15 lines
arch/x86/sched.c +0 -11
@@ -119,20 +113,12 @@ struct thread* arch_create_thread(char *name, void (*function)(void *),
 
 void run_idle_thread(void)
 {
-    /* Switch stacks and run the thread */ 
-#if defined(__i386__)
-    __asm__ __volatile__("mov %0,%%esp\n\t"
-                         "push %1\n\t" 
-                         "ret"                                            
-                         :"=m" (idle_thread->sp)
-                         :"m" (idle_thread->ip));                          
-#elif defined(__x86_64__)
-    __asm__ __volatile__("mov %0,%%rsp\n\t"
-                         "push %1\n\t" 
-                         "ret"                                            

arch/x86/sched.c struct thread* arch_create_thread(char *name, void (*function)(void *), · line 122

Quoted 10 lines
arch/x86/sched.c +6 -3
-                         :"=m" (idle_thread->sp)
-                         :"m" (idle_thread->ip));                                                    
-#endif
+    /* Switch stacks and run the thread */
+    asm volatile ("mov %[sp], %%"ASM_SP"\n\t"
+                  "jmp *%[ip]\n\t"
+                  :
+                  : [sp] "m" (idle_thread->sp),
+                    [ip] "m" (idle_thread->ip));
 }

Here instead you look to be switching to Linux style. Was that intended?

As an aside, I think the construct is slightly problematic: In principle the compiler could make a copy of idle_thread->ip on the stack. (It won't normally, for code efficiency reasons.) That would break with the earlier change of the stack pointer. Using an "r" constraint would perhaps be better there. Yet if so wanted, that would certainly be a separate change.

With the adjustments (or respective clarifications as to style intentions), which I'd be fine making while committing so long as you agree:

Jan

#2 lore raw
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Juergen Gross
arch/x86/sched.c +0 -9
@@ -60,16 +60,10 @@ void dump_stack(struct thread *thread)
      unsigned long *bottom = (unsigned long *)(thread->stack + STACK_SIZE);
      unsigned long *pointer = (unsigned long *)thread->sp;
      int count;
-    if(thread == current)
-    {
-#ifdef __i386__
-        asm("movl %%esp,%0"
-            : "=r"(pointer));
-#else
-        asm("movq %%rsp,%0"
-            : "=r"(pointer));
-#endif
Quoted 8 lines

arch/x86/sched.c void dump_stack(struct thread *thread) · line 65

Quoted 4 lines
arch/x86/sched.c +3 -1
-    }
+
+    if ( thread == current )
+        asm("mov %%"ASM_SP",%0" : "=r"(pointer));

As you switch the if() to Xen style, why not also the asm()? Irrespective of which precise style is meant to be used, the last closing double quote likely also wants to be followed by a blank?

Yes, indeed.

Quoted 20 lines
Quoted 20 lines
arch/x86/sched.c +1 -14
@@ -119,20 +113,12 @@ struct thread* arch_create_thread(char *name, void (*function)(void *),
 
  void run_idle_thread(void)
  {
-    /* Switch stacks and run the thread */
-#if defined(__i386__)
-    __asm__ __volatile__("mov %0,%%esp\n\t"
-                         "push %1\n\t"
-                         "ret"
-                         :"=m" (idle_thread->sp)
-                         :"m" (idle_thread->ip));
-#elif defined(__x86_64__)
-    __asm__ __volatile__("mov %0,%%rsp\n\t"
-                         "push %1\n\t"
-                         "ret"
-                         :"=m" (idle_thread->sp)
-                         :"m" (idle_thread->ip));
-#endif
+    /* Switch stacks and run the thread */
Quoted 8 lines

arch/x86/sched.c struct thread* arch_create_thread(char *name, void (*function)(void *), · line 122

Quoted 6 lines
arch/x86/sched.c +5 -0
+    asm volatile ("mov %[sp], %%"ASM_SP"\n\t"
+                  "jmp *%[ip]\n\t"
+                  :
+                  : [sp] "m" (idle_thread->sp),
+                    [ip] "m" (idle_thread->ip));
  }

Here instead you look to be switching to Linux style. Was that intended?

No, not really. I took Andrew's suggestion verbatim.

Quoted 10 lines

As an aside, I think the construct is slightly problematic: In principle the compiler could make a copy of idle_thread->ip on the stack. (It won't normally, for code efficiency reasons.) That would break with the earlier change of the stack pointer. Using an "r" constraint would perhaps be better there. Yet if so wanted, that would certainly be a separate change.

With the adjustments (or respective clarifications as to style intentions), which I'd be fine making while committing so long as you agree:

Thanks. I agree with the suggested changes (to spell it out explicitly: I meant to use Xen coding style, as this is the Mini-OS style, too).

Juergen