1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: src/sys/i386/i386/exception.s,v 1.65.2.3 2001/08/15 01:23:49 peter Exp $
30 */
31
32#include <machine/asmacros.h>
33#include <machine/segments.h>
34#include <machine/lock.h>
35#include <machine/psl.h>
36#include <machine/trap.h>
37
38#include "assym.s"
39
40	.text
41
42	.globl	lwkt_switch_return
43
44/*
45 * This function is what cpu_heavy_restore jumps to after a new process
46 * is created.  The LWKT subsystem switches while holding a critical
47 * section and we maintain that abstraction here (e.g. because
48 * cpu_heavy_restore needs it due to PCB_*() manipulation), then get out of
49 * it before calling the initial function (typically fork_return()) and/or
50 * returning to user mode.
51 *
52 * The MP lock is not held at any point but the critcount is bumped
53 * on entry to prevent interruption of the trampoline at a bad point.
54 *
55 * This is effectively what td->td_switch() returns to.  It 'returns' the
56 * old thread in %eax and since this is not returning to a td->td_switch()
57 * call from lwkt_switch() we must handle the cleanup for the old thread
58 * by calling lwkt_switch_return().
59 *
60 * fork_trampoline(%rax:otd, %rbp:func, %r12:arg)
61 */
62ENTRY(fork_trampoline)
63	movq	%rax,%rdi
64	call	lwkt_switch_return
65	movq	PCPU(curthread),%rax
66	decl	TD_CRITCOUNT(%rax)
67
68	/*
69	 * cpu_set_fork_handler intercepts this function call to
70	 * have this call a non-return function to stay in kernel mode.
71	 *
72	 * initproc has its own fork handler, start_init(), which DOES
73	 * return.
74	 *
75	 * %rbx - chaining function (typically fork_return)
76	 * %r12 -> %rdi (argument)
77	 * frame-> %rsi (trap frame)
78	 *
79	 *   void (func:rbx)(arg:rdi, trapframe:rsi)
80	 */
81	movq	%rsp, %rsi		/* pass trapframe by reference */
82	movq	%r12, %rdi		/* arg1 */
83	call	*%rbx			/* function */
84
85	/* cut from syscall */
86
87	call	splz
88
89	/*
90	 * Return via doreti to handle ASTs.
91	 *
92	 * trapframe is at the top of the stack.
93	 */
94	MEXITCOUNT
95	pushq	$0		/* if_vec */
96	movq	%rsp,%rdi	/* pass by reference */
97	call	go_user
98	/* NOT REACHED */
99