On Wed, Jul 18, 2012 at 11:10:03AM +0300, Konstantin Belousov wrote:
> On Wed, Jul 18, 2012 at 04:59:38PM +1000, Bruce Evans wrote:
> > On Wed, 18 Jul 2012, Konstantin Belousov wrote:
> > Putting the definiton in machine/pcpu.h should avoid changing the
> > prerequistes for machine/pcb.h.
> > 
> > >#ifndef _KERNEL
> > >/* stuff that *used* to be included by user.h, or is now needed */
> > >
> > >Please note the location in pcb.h an not in machine/pcpu.h, where it
> > >cannot work for technical reasons (struct pcpu is not defined yet).
> > 
> > Not applicable -- see above.
> No, this cannot work. machine/pcpu.h defines PCPU_MD_FIELDS which is used
> to provide md part of the struct pcpu.

Below is the tested patch which converts all users of PCPU_GET(curpcb)
for amd64.

diff --git a/sys/amd64/amd64/fpu.c b/sys/amd64/amd64/fpu.c
index f88aba7..fe4bcf1 100644
--- a/sys/amd64/amd64/fpu.c
+++ b/sys/amd64/amd64/fpu.c
@@ -327,7 +327,7 @@ fpuexit(struct thread *td)
        critical_enter();
        if (curthread == PCPU_GET(fpcurthread)) {
                stop_emulating();
-               fpusave(PCPU_GET(curpcb)->pcb_save);
+               fpusave(curpcb->pcb_save);
                start_emulating();
                PCPU_SET(fpcurthread, 0);
        }
@@ -547,7 +547,7 @@ fputrap_x87(void)
         * wherever they are.
         */
        if (PCPU_GET(fpcurthread) != curthread) {
-               pcb_save = PCPU_GET(curpcb)->pcb_save;
+               pcb_save = curpcb->pcb_save;
                control = pcb_save->sv_env.en_cw;
                status = pcb_save->sv_env.en_sw;
        } else {
@@ -567,7 +567,7 @@ fputrap_sse(void)
 
        critical_enter();
        if (PCPU_GET(fpcurthread) != curthread)
-               mxcsr = PCPU_GET(curpcb)->pcb_save->sv_env.en_mxcsr;
+               mxcsr = curpcb->pcb_save->sv_env.en_mxcsr;
        else
                stmxcsr(&mxcsr);
        critical_exit();
@@ -609,7 +609,7 @@ fpudna(void)
         * Record new context early in case frstor causes a trap.
         */
        PCPU_SET(fpcurthread, curthread);
-       pcb = PCPU_GET(curpcb);
+       pcb = curpcb;
 
        fpu_clean_state();
 
@@ -970,7 +970,7 @@ fpu_kern_thread(u_int flags)
 {
        struct pcb *pcb;
 
-       pcb = PCPU_GET(curpcb);
+       pcb = curpcb;
        KASSERT((curthread->td_pflags & TDP_KTHREAD) != 0,
            ("Only kthread may use fpu_kern_thread"));
        KASSERT(pcb->pcb_save == get_pcb_user_save_pcb(pcb),
@@ -987,5 +987,5 @@ is_fpu_kern_thread(u_int flags)
 
        if ((curthread->td_pflags & TDP_KTHREAD) == 0)
                return (0);
-       return ((PCPU_GET(curpcb)->pcb_flags & PCB_KERNFPU) != 0);
+       return ((curpcb->pcb_flags & PCB_KERNFPU) != 0);
 }
diff --git a/sys/amd64/amd64/machdep.c b/sys/amd64/amd64/machdep.c
index 8044fe5..cd3ebc5 100644
--- a/sys/amd64/amd64/machdep.c
+++ b/sys/amd64/amd64/machdep.c
@@ -996,7 +996,7 @@ exec_setregs(struct thread *td, struct image_params *imgp, 
u_long stack)
                pcb->pcb_dr3 = 0;
                pcb->pcb_dr6 = 0;
                pcb->pcb_dr7 = 0;
-               if (pcb == PCPU_GET(curpcb)) {
+               if (pcb == curpcb) {
                        /*
                         * Clear the debug registers on the running
                         * CPU, otherwise they will end up affecting
diff --git a/sys/amd64/amd64/trap.c b/sys/amd64/amd64/trap.c
index 57d1cc2..d035a7e 100644
--- a/sys/amd64/amd64/trap.c
+++ b/sys/amd64/amd64/trap.c
@@ -520,9 +520,8 @@ trap(struct trapframe *frame)
                                frame->tf_rip = (long)fsbase_load_fault;
                                goto out;
                        }
-                       if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
-                               frame->tf_rip =
-                                   (long)PCPU_GET(curpcb)->pcb_onfault;
+                       if (curpcb->pcb_onfault != NULL) {
+                               frame->tf_rip = (long)curpcb->pcb_onfault;
                                goto out;
                        }
                        break;
@@ -708,7 +707,7 @@ trap_pfault(frame, usermode)
                 * it normally, and panic immediately.
                 */
                if (!usermode && (td->td_intr_nesting_level != 0 ||
-                   PCPU_GET(curpcb)->pcb_onfault == NULL)) {
+                   curpcb->pcb_onfault == NULL)) {
                        trap_fatal(frame, eva);
                        return (-1);
                }
@@ -764,8 +763,8 @@ trap_pfault(frame, usermode)
 nogo:
        if (!usermode) {
                if (td->td_intr_nesting_level == 0 &&
-                   PCPU_GET(curpcb)->pcb_onfault != NULL) {
-                       frame->tf_rip = (long)PCPU_GET(curpcb)->pcb_onfault;
+                   curpcb->pcb_onfault != NULL) {
+                       frame->tf_rip = (long)curpcb->pcb_onfault;
                        return (0);
                }
                trap_fatal(frame, eva);
diff --git a/sys/amd64/include/pcb.h b/sys/amd64/include/pcb.h
index 22cbbe2..d814902 100644
--- a/sys/amd64/include/pcb.h
+++ b/sys/amd64/include/pcb.h
@@ -144,6 +144,17 @@ void       makectx(struct trapframe *, struct pcb *);
 int    savectx(struct pcb *) __returns_twice;
 void   resumectx(struct pcb *);
 
+static __inline __pure2 struct pcb *
+__curpcb(void)
+{
+       struct pcb *pcb;
+
+       __asm("movq %%gs:%1,%0" : "=r" (pcb)
+           : "m" (((struct pcpu *)NULL)->pc_curpcb));
+       return (pcb);
+}
+#define        curpcb          (__curpcb())
+
 #endif
 
 #endif /* _AMD64_PCB_H_ */
diff --git a/sys/sys/user.h b/sys/sys/user.h
index accb7c3..ab1c7a7 100644
--- a/sys/sys/user.h
+++ b/sys/sys/user.h
@@ -35,6 +35,7 @@
 #ifndef _SYS_USER_H_
 #define _SYS_USER_H_
 
+#include <sys/pcpu.h>
 #include <machine/pcb.h>
 #ifndef _KERNEL
 /* stuff that *used* to be included by user.h, or is now needed */

Attachment: pgpuLeDQz6He5.pgp
Description: PGP signature

Reply via email to