fix:在smp模块中增加mfence

This commit is contained in:
fslongjin 2022-08-01 23:18:54 +08:00
parent 83152c176b
commit 8bd6e981f0
8 changed files with 136 additions and 71 deletions

View File

@ -10,7 +10,7 @@ LIB_FILES := $(foreach DIR,$(DIR_LIB),$(addprefix $(DIR)/,$(lib_patterns)))
# 控制操作系统使用的中断控制器 _INTR_8259A_ _INTR_APIC_ # 控制操作系统使用的中断控制器 _INTR_8259A_ _INTR_APIC_
PIC := _INTR_APIC_ PIC := _INTR_APIC_
CFLAGS = $(GLOBAL_CFLAGS) -D $(PIC) -I $(shell pwd) CFLAGS = $(GLOBAL_CFLAGS) -D $(PIC) -I $(shell pwd) -O3
export ASFLAGS := --64 export ASFLAGS := --64

View File

@ -8,8 +8,7 @@
extern spinlock_t xhci_controller_init_lock; // xhci控制器初始化锁 extern spinlock_t xhci_controller_init_lock; // xhci控制器初始化锁
#define MAX_USB_NUM 8 // pci总线上的usb设备的最大数量 #define MAX_USB_NUM 8 // pci总线上的usb设备的最大数量
#pragma GCC push_options
#pragma GCC optimize("O0")
// 在pci总线上寻找到的usb设备控制器的header // 在pci总线上寻找到的usb设备控制器的header
static struct pci_device_structure_header_t *usb_pdevs[MAX_USB_NUM]; static struct pci_device_structure_header_t *usb_pdevs[MAX_USB_NUM];
static int usb_pdevs_count = 0; static int usb_pdevs_count = 0;
@ -35,6 +34,7 @@ void usb_init()
// 初始化每个usb控制器 // 初始化每个usb控制器
for (int i = 0; i < usb_pdevs_count; ++i) for (int i = 0; i < usb_pdevs_count; ++i)
{ {
io_mfence();
switch (usb_pdevs[i]->ProgIF) switch (usb_pdevs[i]->ProgIF)
{ {
case USB_TYPE_UHCI: case USB_TYPE_UHCI:
@ -48,6 +48,7 @@ void usb_init()
case USB_TYPE_XHCI: case USB_TYPE_XHCI:
// 初始化对应的xhci控制器 // 初始化对应的xhci控制器
xhci_init((struct pci_device_structure_general_device_t *)usb_pdevs[i]); xhci_init((struct pci_device_structure_general_device_t *)usb_pdevs[i]);
io_mfence();
break; break;
default: default:
@ -58,4 +59,3 @@ void usb_init()
} }
kinfo("Successfully initialized all usb host controllers!"); kinfo("Successfully initialized all usb host controllers!");
} }
#pragma GCC pop_options

View File

@ -56,7 +56,6 @@ hardware_intr_controller xhci_hc_intr_controller =
32bit的寄存器中的偏移量8的位置开始读取1个字节 32bit的寄存器中的偏移量8的位置开始读取1个字节
32bit的寄存器的0地址处开始读取32bit 32bit的寄存器的0地址处开始读取32bit
*/ */
#define xhci_read_cap_reg8(id, offset) (*(uint8_t *)(xhci_hc[id].vbase + offset)) #define xhci_read_cap_reg8(id, offset) (*(uint8_t *)(xhci_hc[id].vbase + offset))
#define xhci_get_ptr_cap_reg8(id, offset) ((uint8_t *)(xhci_hc[id].vbase + offset)) #define xhci_get_ptr_cap_reg8(id, offset) ((uint8_t *)(xhci_hc[id].vbase + offset))
#define xhci_write_cap_reg8(id, offset, value) (*(uint8_t *)(xhci_hc[id].vbase + offset) = (uint8_t)value) #define xhci_write_cap_reg8(id, offset, value) (*(uint8_t *)(xhci_hc[id].vbase + offset) = (uint8_t)value)
@ -199,11 +198,13 @@ static int xhci_hc_stop(int id)
// 判断是否已经停止 // 判断是否已经停止
if (unlikely((xhci_read_op_reg32(id, XHCI_OPS_USBSTS) & (1 << 0)) == 1)) if (unlikely((xhci_read_op_reg32(id, XHCI_OPS_USBSTS) & (1 << 0)) == 1))
return 0; return 0;
io_mfence();
xhci_write_op_reg32(id, XHCI_OPS_USBCMD, 0x00000000); xhci_write_op_reg32(id, XHCI_OPS_USBCMD, 0x00000000);
io_mfence();
char timeout = 17; char timeout = 17;
while ((xhci_read_op_reg32(id, XHCI_OPS_USBSTS) & (1 << 0)) == 0) while ((xhci_read_op_reg32(id, XHCI_OPS_USBSTS) & (1 << 0)) == 0)
{ {
io_mfence();
usleep(1000); usleep(1000);
if (--timeout == 0) if (--timeout == 0)
return -ETIMEDOUT; return -ETIMEDOUT;
@ -222,9 +223,11 @@ static int xhci_hc_reset(int id)
{ {
int retval = 0; int retval = 0;
kdebug("usbsts=%#010lx", xhci_read_op_reg32(id, XHCI_OPS_USBSTS)); kdebug("usbsts=%#010lx", xhci_read_op_reg32(id, XHCI_OPS_USBSTS));
io_mfence();
// 判断HCHalted是否置位 // 判断HCHalted是否置位
if ((xhci_read_op_reg32(id, XHCI_OPS_USBSTS) & (1 << 0)) == 0) if ((xhci_read_op_reg32(id, XHCI_OPS_USBSTS) & (1 << 0)) == 0)
{ {
io_mfence();
kdebug("stopping usb hc..."); kdebug("stopping usb hc...");
// 未置位需要先尝试停止usb主机控制器 // 未置位需要先尝试停止usb主机控制器
retval = xhci_hc_stop(id); retval = xhci_hc_stop(id);
@ -234,12 +237,16 @@ static int xhci_hc_reset(int id)
int timeout = 500; // wait 500ms int timeout = 500; // wait 500ms
// reset // reset
uint32_t cmd = xhci_read_op_reg32(id, XHCI_OPS_USBCMD); uint32_t cmd = xhci_read_op_reg32(id, XHCI_OPS_USBCMD);
io_mfence();
kdebug("cmd=%#010lx", cmd); kdebug("cmd=%#010lx", cmd);
cmd |= (1 << 1); cmd |= (1 << 1);
xhci_write_op_reg32(id, XHCI_OPS_USBCMD, cmd); xhci_write_op_reg32(id, XHCI_OPS_USBCMD, cmd);
io_mfence();
kdebug("after rst, sts=%#010lx", xhci_read_op_reg32(id, XHCI_OPS_USBSTS)); kdebug("after rst, sts=%#010lx", xhci_read_op_reg32(id, XHCI_OPS_USBSTS));
io_mfence();
while (xhci_read_op_reg32(id, XHCI_OPS_USBCMD) & (1 << 1)) while (xhci_read_op_reg32(id, XHCI_OPS_USBCMD) & (1 << 1))
{ {
io_mfence();
usleep(1000); usleep(1000);
if (--timeout == 0) if (--timeout == 0)
return -ETIMEDOUT; return -ETIMEDOUT;
@ -263,14 +270,15 @@ static int xhci_hc_stop_legacy(int id)
// 判断当前entry是否为legacy support entry // 判断当前entry是否为legacy support entry
if (xhci_read_cap_reg8(id, current_offset) == XHCI_XECP_ID_LEGACY) if (xhci_read_cap_reg8(id, current_offset) == XHCI_XECP_ID_LEGACY)
{ {
io_mfence();
// 接管控制权 // 接管控制权
xhci_write_cap_reg32(id, current_offset, xhci_read_cap_reg32(id, current_offset) | XHCI_XECP_LEGACY_OS_OWNED); xhci_write_cap_reg32(id, current_offset, xhci_read_cap_reg32(id, current_offset) | XHCI_XECP_LEGACY_OS_OWNED);
io_mfence();
// 等待响应完成 // 等待响应完成
int timeout = XHCI_XECP_LEGACY_TIMEOUT; int timeout = XHCI_XECP_LEGACY_TIMEOUT;
while ((xhci_read_cap_reg32(id, current_offset) & XHCI_XECP_LEGACY_OWNING_MASK) != XHCI_XECP_LEGACY_OS_OWNED) while ((xhci_read_cap_reg32(id, current_offset) & XHCI_XECP_LEGACY_OWNING_MASK) != XHCI_XECP_LEGACY_OS_OWNED)
{ {
io_mfence();
usleep(1000); usleep(1000);
if (--timeout == 0) if (--timeout == 0)
{ {
@ -281,9 +289,10 @@ static int xhci_hc_stop_legacy(int id)
// 处理完成 // 处理完成
return 0; return 0;
} }
io_mfence();
// 读取下一个entry的偏移增加量 // 读取下一个entry的偏移增加量
int next_off = ((xhci_read_cap_reg32(id, current_offset) & 0xff00) >> 8) << 2; int next_off = ((xhci_read_cap_reg32(id, current_offset) & 0xff00) >> 8) << 2;
io_mfence();
// 将指针跳转到下一个entry // 将指针跳转到下一个entry
current_offset = next_off ? (current_offset + next_off) : 0; current_offset = next_off ? (current_offset + next_off) : 0;
} while (current_offset); } while (current_offset);
@ -300,7 +309,9 @@ static int xhci_hc_stop_legacy(int id)
*/ */
static int xhci_hc_start_sched(int id) static int xhci_hc_start_sched(int id)
{ {
io_mfence();
xhci_write_op_reg32(id, XHCI_OPS_USBCMD, (1 << 0) | (1 >> 2) | (1 << 3)); xhci_write_op_reg32(id, XHCI_OPS_USBCMD, (1 << 0) | (1 >> 2) | (1 << 3));
io_mfence();
usleep(100 * 1000); usleep(100 * 1000);
} }
@ -312,7 +323,9 @@ static int xhci_hc_start_sched(int id)
*/ */
static int xhci_hc_stop_sched(int id) static int xhci_hc_stop_sched(int id)
{ {
io_mfence();
xhci_write_op_reg32(id, XHCI_OPS_USBCMD, 0x00); xhci_write_op_reg32(id, XHCI_OPS_USBCMD, 0x00);
io_mfence();
} }
/** /**
@ -340,13 +353,14 @@ static uint32_t xhci_hc_get_protocol_offset(int id, uint32_t list_off, const int
do do
{ {
uint32_t dw0 = xhci_read_cap_reg32(id, list_off); uint32_t dw0 = xhci_read_cap_reg32(id, list_off);
io_mfence();
uint32_t next_list_off = (dw0 >> 8) & 0xff; uint32_t next_list_off = (dw0 >> 8) & 0xff;
next_list_off = next_list_off ? (list_off + (next_list_off << 2)) : 0; next_list_off = next_list_off ? (list_off + (next_list_off << 2)) : 0;
if ((dw0 & 0xff) == XHCI_XECP_ID_PROTOCOL && ((dw0 & 0xff000000) >> 24) == version) if ((dw0 & 0xff) == XHCI_XECP_ID_PROTOCOL && ((dw0 & 0xff000000) >> 24) == version)
{ {
uint32_t dw2 = xhci_read_cap_reg32(id, list_off + 8); uint32_t dw2 = xhci_read_cap_reg32(id, list_off + 8);
io_mfence();
if (offset != NULL) if (offset != NULL)
*offset = (uint32_t)(dw2 & 0xff) - 1; // 使其转换为zero based *offset = (uint32_t)(dw2 & 0xff) - 1; // 使其转换为zero based
if (count != NULL) if (count != NULL)
@ -372,8 +386,9 @@ static int xhci_hc_pair_ports(int id)
{ {
struct xhci_caps_HCSPARAMS1_reg_t hcs1; struct xhci_caps_HCSPARAMS1_reg_t hcs1;
io_mfence();
memcpy(&hcs1, xhci_get_ptr_cap_reg32(id, XHCI_CAPS_HCSPARAMS1), sizeof(struct xhci_caps_HCSPARAMS1_reg_t)); memcpy(&hcs1, xhci_get_ptr_cap_reg32(id, XHCI_CAPS_HCSPARAMS1), sizeof(struct xhci_caps_HCSPARAMS1_reg_t));
io_mfence();
// 从hcs1获取端口数量 // 从hcs1获取端口数量
xhci_hc[id].port_num = hcs1.max_ports; xhci_hc[id].port_num = hcs1.max_ports;
@ -389,15 +404,18 @@ static int xhci_hc_pair_ports(int id)
// 寻找所有的usb2端口 // 寻找所有的usb2端口
while (next_off) while (next_off)
{ {
io_mfence();
next_off = xhci_hc_get_protocol_offset(id, next_off, 2, &offset, &cnt, &protocol_flags); next_off = xhci_hc_get_protocol_offset(id, next_off, 2, &offset, &cnt, &protocol_flags);
io_mfence();
if (cnt) if (cnt)
{ {
for (int i = 0; i < cnt; ++i) for (int i = 0; i < cnt; ++i)
{ {
io_mfence();
xhci_hc[id].ports[offset + i].offset = xhci_hc[id].port_num_u2++; xhci_hc[id].ports[offset + i].offset = xhci_hc[id].port_num_u2++;
xhci_hc[id].ports[offset + i].flags = XHCI_PROTOCOL_USB2; xhci_hc[id].ports[offset + i].flags = XHCI_PROTOCOL_USB2;
io_mfence();
// usb2 high speed only // usb2 high speed only
if (protocol_flags & 2) if (protocol_flags & 2)
xhci_hc[id].ports[offset + i].flags |= XHCI_PROTOCOL_HSO; xhci_hc[id].ports[offset + i].flags |= XHCI_PROTOCOL_HSO;
@ -409,12 +427,15 @@ static int xhci_hc_pair_ports(int id)
next_off = xhci_hc[id].ext_caps_off; next_off = xhci_hc[id].ext_caps_off;
while (next_off) while (next_off)
{ {
io_mfence();
next_off = xhci_hc_get_protocol_offset(id, next_off, 3, &offset, &cnt, &protocol_flags); next_off = xhci_hc_get_protocol_offset(id, next_off, 3, &offset, &cnt, &protocol_flags);
io_mfence();
if (cnt) if (cnt)
{ {
for (int i = 0; i < cnt; ++i) for (int i = 0; i < cnt; ++i)
{ {
io_mfence();
xhci_hc[id].ports[offset + i].offset = xhci_hc[id].port_num_u3++; xhci_hc[id].ports[offset + i].offset = xhci_hc[id].port_num_u3++;
xhci_hc[id].ports[offset + i].flags = XHCI_PROTOCOL_USB3; xhci_hc[id].ports[offset + i].flags = XHCI_PROTOCOL_USB3;
} }
@ -428,13 +449,13 @@ static int xhci_hc_pair_ports(int id)
{ {
if (unlikely(i == j)) if (unlikely(i == j))
continue; continue;
io_mfence();
if ((xhci_hc[id].ports[i].offset == xhci_hc[id].ports[j].offset) && if ((xhci_hc[id].ports[i].offset == xhci_hc[id].ports[j].offset) &&
((xhci_hc[id].ports[i].flags & XHCI_PROTOCOL_INFO) != (xhci_hc[id].ports[j].flags & XHCI_PROTOCOL_INFO))) ((xhci_hc[id].ports[i].flags & XHCI_PROTOCOL_INFO) != (xhci_hc[id].ports[j].flags & XHCI_PROTOCOL_INFO)))
{ {
xhci_hc[id].ports[i].paired_port_num = j; xhci_hc[id].ports[i].paired_port_num = j;
xhci_hc[id].ports[i].flags |= XHCI_PROTOCOL_HAS_PAIR; xhci_hc[id].ports[i].flags |= XHCI_PROTOCOL_HAS_PAIR;
io_mfence();
xhci_hc[id].ports[j].paired_port_num = i; xhci_hc[id].ports[j].paired_port_num = i;
xhci_hc[id].ports[j].flags |= XHCI_PROTOCOL_HAS_PAIR; xhci_hc[id].ports[j].flags |= XHCI_PROTOCOL_HAS_PAIR;
} }
@ -444,6 +465,7 @@ static int xhci_hc_pair_ports(int id)
// 标记所有的usb3、单独的usb2端口为激活状态 // 标记所有的usb3、单独的usb2端口为激活状态
for (int i = 0; i < xhci_hc[id].port_num; ++i) for (int i = 0; i < xhci_hc[id].port_num; ++i)
{ {
io_mfence();
if (XHCI_PORT_IS_USB3(id, i) || if (XHCI_PORT_IS_USB3(id, i) ||
(XHCI_PORT_IS_USB2(id, i) && (!XHCI_PORT_HAS_PAIR(id, i)))) (XHCI_PORT_IS_USB2(id, i) && (!XHCI_PORT_HAS_PAIR(id, i))))
xhci_hc[id].ports[i].flags |= XHCI_PROTOCOL_ACTIVE; xhci_hc[id].ports[i].flags |= XHCI_PROTOCOL_ACTIVE;
@ -485,11 +507,12 @@ static uint64_t xhci_create_ring(int trbs)
{ {
int total_size = trbs * sizeof(struct xhci_TRB_t); int total_size = trbs * sizeof(struct xhci_TRB_t);
const uint64_t vaddr = (uint64_t)kmalloc(total_size, 0); const uint64_t vaddr = (uint64_t)kmalloc(total_size, 0);
io_mfence();
memset((void *)vaddr, 0, total_size); memset((void *)vaddr, 0, total_size);
io_mfence();
// 设置最后一个trb为link trb // 设置最后一个trb为link trb
xhci_TRB_set_link_cmd(vaddr + total_size - sizeof(struct xhci_TRB_t)); xhci_TRB_set_link_cmd(vaddr + total_size - sizeof(struct xhci_TRB_t));
io_mfence();
return vaddr; return vaddr;
} }
@ -503,18 +526,19 @@ static uint64_t xhci_create_ring(int trbs)
static uint64_t xhci_create_event_ring(int trbs, uint64_t *ret_ring_addr) static uint64_t xhci_create_event_ring(int trbs, uint64_t *ret_ring_addr)
{ {
const uint64_t table_vaddr = (const uint64_t)kmalloc(64, 0); // table支持8个segment const uint64_t table_vaddr = (const uint64_t)kmalloc(64, 0); // table支持8个segment
io_mfence();
if (unlikely(table_vaddr == NULL)) if (unlikely(table_vaddr == NULL))
return -ENOMEM; return -ENOMEM;
memset((void *)table_vaddr, 0, 64); memset((void *)table_vaddr, 0, 64);
// 暂时只创建1个segment // 暂时只创建1个segment
const uint64_t seg_vaddr = (const uint64_t)kmalloc(trbs * sizeof(struct xhci_TRB_t), 0); const uint64_t seg_vaddr = (const uint64_t)kmalloc(trbs * sizeof(struct xhci_TRB_t), 0);
io_mfence();
if (unlikely(seg_vaddr == NULL)) if (unlikely(seg_vaddr == NULL))
return -ENOMEM; return -ENOMEM;
memset((void *)seg_vaddr, 0, trbs * sizeof(struct xhci_TRB_t)); memset((void *)seg_vaddr, 0, trbs * sizeof(struct xhci_TRB_t));
io_mfence();
// 将segment地址和大小写入table // 将segment地址和大小写入table
*(uint64_t *)(table_vaddr) = virt_2_phys(seg_vaddr); *(uint64_t *)(table_vaddr) = virt_2_phys(seg_vaddr);
*(uint64_t *)(table_vaddr + 8) = trbs; *(uint64_t *)(table_vaddr + 8) = trbs;
@ -526,13 +550,17 @@ static uint64_t xhci_create_event_ring(int trbs, uint64_t *ret_ring_addr)
void xhci_hc_irq_enable(uint64_t irq_num) void xhci_hc_irq_enable(uint64_t irq_num)
{ {
int cid = xhci_find_hcid_by_irq_num(irq_num); int cid = xhci_find_hcid_by_irq_num(irq_num);
io_mfence();
if (WARN_ON(cid == -1)) if (WARN_ON(cid == -1))
return; return;
kdebug("start msi"); kdebug("start msi");
io_mfence();
pci_start_msi(xhci_hc[cid].pci_dev_hdr); pci_start_msi(xhci_hc[cid].pci_dev_hdr);
kdebug("start sched"); kdebug("start sched");
io_mfence();
xhci_hc_start_sched(cid); xhci_hc_start_sched(cid);
kdebug("start ports"); kdebug("start ports");
io_mfence();
xhci_hc_start_ports(cid); xhci_hc_start_ports(cid);
kdebug("enabled"); kdebug("enabled");
} }
@ -540,29 +568,34 @@ void xhci_hc_irq_enable(uint64_t irq_num)
void xhci_hc_irq_disable(uint64_t irq_num) void xhci_hc_irq_disable(uint64_t irq_num)
{ {
int cid = xhci_find_hcid_by_irq_num(irq_num); int cid = xhci_find_hcid_by_irq_num(irq_num);
io_mfence();
if (WARN_ON(cid == -1)) if (WARN_ON(cid == -1))
return; return;
xhci_hc_stop_sched(cid); xhci_hc_stop_sched(cid);
io_mfence();
pci_disable_msi(xhci_hc[cid].pci_dev_hdr); pci_disable_msi(xhci_hc[cid].pci_dev_hdr);
io_mfence();
} }
uint64_t xhci_hc_irq_install(uint64_t irq_num, void *arg) uint64_t xhci_hc_irq_install(uint64_t irq_num, void *arg)
{ {
int cid = xhci_find_hcid_by_irq_num(irq_num); int cid = xhci_find_hcid_by_irq_num(irq_num);
io_mfence();
if (WARN_ON(cid == -1)) if (WARN_ON(cid == -1))
return -EINVAL; return -EINVAL;
struct xhci_hc_irq_install_info_t *info = (struct xhci_hc_irq_install_info_t *)arg; struct xhci_hc_irq_install_info_t *info = (struct xhci_hc_irq_install_info_t *)arg;
struct msi_desc_t msi_desc; struct msi_desc_t msi_desc;
memset(&msi_desc, 0, sizeof(struct msi_desc_t)); memset(&msi_desc, 0, sizeof(struct msi_desc_t));
io_mfence();
msi_desc.pci_dev = (struct pci_device_structure_header_t *)xhci_hc[cid].pci_dev_hdr; msi_desc.pci_dev = (struct pci_device_structure_header_t *)xhci_hc[cid].pci_dev_hdr;
msi_desc.assert = info->assert; msi_desc.assert = info->assert;
msi_desc.edge_trigger = info->edge_trigger; msi_desc.edge_trigger = info->edge_trigger;
msi_desc.processor = info->processor; msi_desc.processor = info->processor;
msi_desc.pci.msi_attribute.is_64 = 1; msi_desc.pci.msi_attribute.is_64 = 1;
// todo: QEMU是使用msix的因此要先在pci中实现msix // todo: QEMU是使用msix的因此要先在pci中实现msix
io_mfence();
int retval = pci_enable_msi(&msi_desc); int retval = pci_enable_msi(&msi_desc);
kdebug("pci retval = %d", retval); kdebug("pci retval = %d", retval);
kdebug("xhci irq %d installed.", irq_num); kdebug("xhci irq %d installed.", irq_num);
@ -573,9 +606,11 @@ void xhci_hc_irq_uninstall(uint64_t irq_num)
{ {
// todo // todo
int cid = xhci_find_hcid_by_irq_num(irq_num); int cid = xhci_find_hcid_by_irq_num(irq_num);
io_mfence();
if (WARN_ON(cid == -1)) if (WARN_ON(cid == -1))
return; return;
xhci_hc_stop(cid); xhci_hc_stop(cid);
io_mfence();
} }
/** /**
* @brief xhci主机控制器的中断处理函数 * @brief xhci主机控制器的中断处理函数
@ -603,11 +638,14 @@ static int xhci_reset_port(const int id, const int port)
// 相对于op寄存器基地址的偏移量 // 相对于op寄存器基地址的偏移量
uint64_t port_status_offset = XHCI_OPS_PRS + port * 16; uint64_t port_status_offset = XHCI_OPS_PRS + port * 16;
// kdebug("to reset %d, offset=%#018lx", port, port_status_offset); // kdebug("to reset %d, offset=%#018lx", port, port_status_offset);
io_mfence();
// 检查端口电源状态 // 检查端口电源状态
if ((xhci_read_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC) & (1 << 9)) == 0) if ((xhci_read_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC) & (1 << 9)) == 0)
{ {
kdebug("port is power off, starting..."); kdebug("port is power off, starting...");
io_mfence();
xhci_write_cap_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9)); xhci_write_cap_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9));
io_mfence();
usleep(2000); usleep(2000);
// 检测端口是否被启用, 若未启用,则报错 // 检测端口是否被启用, 若未启用,则报错
if ((xhci_read_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC) & (1 << 9)) == 0) if ((xhci_read_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC) & (1 << 9)) == 0)
@ -617,10 +655,10 @@ static int xhci_reset_port(const int id, const int port)
} }
} }
// kdebug("port:%d, power check ok", port); // kdebug("port:%d, power check ok", port);
io_mfence();
// 确保端口的status被清0 // 确保端口的status被清0
xhci_write_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9) | XHCI_PORTUSB_CHANGE_BITS); xhci_write_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9) | XHCI_PORTUSB_CHANGE_BITS);
io_mfence();
// 重置当前端口 // 重置当前端口
if (XHCI_PORT_IS_USB3(id, port)) if (XHCI_PORT_IS_USB3(id, port))
xhci_write_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9) | (1 << 31)); xhci_write_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9) | (1 << 31));
@ -633,7 +671,9 @@ static int xhci_reset_port(const int id, const int port)
int timeout = 200; int timeout = 200;
while (timeout) while (timeout)
{ {
io_mfence();
uint32_t val = xhci_read_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC); uint32_t val = xhci_read_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC);
io_mfence();
if (XHCI_PORT_IS_USB3(id, port) && (val & (1 << 31)) == 0) if (XHCI_PORT_IS_USB3(id, port) && (val & (1 << 31)) == 0)
break; break;
else if (XHCI_PORT_IS_USB2(id, port) && (val & (1 << 4)) == 0) else if (XHCI_PORT_IS_USB2(id, port) && (val & (1 << 4)) == 0)
@ -651,12 +691,14 @@ static int xhci_reset_port(const int id, const int port)
// 等待恢复 // 等待恢复
usleep(USB_TIME_RST_REC * 1000); usleep(USB_TIME_RST_REC * 1000);
uint32_t val = xhci_read_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC); uint32_t val = xhci_read_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC);
io_mfence();
// 如果reset之后enable bit仍然是1那么说明reset成功 // 如果reset之后enable bit仍然是1那么说明reset成功
if (val & (1 << 1)) if (val & (1 << 1))
{ {
io_mfence();
// 清除status change bit // 清除status change bit
xhci_write_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9) | XHCI_PORTUSB_CHANGE_BITS); xhci_write_op_reg32(id, port_status_offset + XHCI_PORT_PORTSC, (1 << 9) | XHCI_PORTUSB_CHANGE_BITS);
io_mfence();
} }
retval = 0; retval = 0;
} }
@ -695,6 +737,7 @@ static int xhci_hc_start_ports(int id)
{ {
if (XHCI_PORT_IS_USB3(id, i) && XHCI_PORT_IS_ACTIVE(id, i)) if (XHCI_PORT_IS_USB3(id, i) && XHCI_PORT_IS_ACTIVE(id, i))
{ {
io_mfence();
// reset该端口 // reset该端口
if (likely(xhci_reset_port(id, i) == 0)) // 如果端口reset成功就获取它的描述符 if (likely(xhci_reset_port(id, i) == 0)) // 如果端口reset成功就获取它的描述符
// 否则reset函数会把它给设置为未激活并且标志配对的usb2端口是激活的 // 否则reset函数会把它给设置为未激活并且标志配对的usb2端口是激活的
@ -735,14 +778,18 @@ static int xhci_hc_init_intr(int id)
struct xhci_caps_HCSPARAMS1_reg_t hcs1; struct xhci_caps_HCSPARAMS1_reg_t hcs1;
struct xhci_caps_HCSPARAMS2_reg_t hcs2; struct xhci_caps_HCSPARAMS2_reg_t hcs2;
io_mfence();
memcpy(&hcs1, xhci_get_ptr_cap_reg32(id, XHCI_CAPS_HCSPARAMS1), sizeof(struct xhci_caps_HCSPARAMS1_reg_t)); memcpy(&hcs1, xhci_get_ptr_cap_reg32(id, XHCI_CAPS_HCSPARAMS1), sizeof(struct xhci_caps_HCSPARAMS1_reg_t));
io_mfence();
memcpy(&hcs2, xhci_get_ptr_cap_reg32(id, XHCI_CAPS_HCSPARAMS2), sizeof(struct xhci_caps_HCSPARAMS2_reg_t)); memcpy(&hcs2, xhci_get_ptr_cap_reg32(id, XHCI_CAPS_HCSPARAMS2), sizeof(struct xhci_caps_HCSPARAMS2_reg_t));
io_mfence();
uint32_t max_segs = (1 << (uint32_t)(hcs2.ERST_Max)); uint32_t max_segs = (1 << (uint32_t)(hcs2.ERST_Max));
uint32_t max_interrupters = hcs1.max_intrs; uint32_t max_interrupters = hcs1.max_intrs;
// 创建 event ring // 创建 event ring
retval = xhci_create_event_ring(4096, &xhci_hc[id].event_ring_vaddr); retval = xhci_create_event_ring(4096, &xhci_hc[id].event_ring_vaddr);
io_mfence();
if (unlikely((int64_t)(retval) == -ENOMEM)) if (unlikely((int64_t)(retval) == -ENOMEM))
return -ENOMEM; return -ENOMEM;
xhci_hc[id].event_ring_table_vaddr = retval; xhci_hc[id].event_ring_table_vaddr = retval;
@ -751,15 +798,21 @@ static int xhci_hc_init_intr(int id)
xhci_hc[id].current_event_ring_cycle = 1; xhci_hc[id].current_event_ring_cycle = 1;
// 写入第0个中断寄存器组 // 写入第0个中断寄存器组
io_mfence();
xhci_write_intr_reg32(id, 0, XHCI_IR_MAN, 0x3); // 使能中断并清除pending位这个pending位是写入1就清0的 xhci_write_intr_reg32(id, 0, XHCI_IR_MAN, 0x3); // 使能中断并清除pending位这个pending位是写入1就清0的
io_mfence();
xhci_write_intr_reg32(id, 0, XHCI_IR_MOD, 0); // 关闭中断管制 xhci_write_intr_reg32(id, 0, XHCI_IR_MOD, 0); // 关闭中断管制
io_mfence();
xhci_write_intr_reg32(id, 0, XHCI_IR_TABLE_SIZE, 1); // 当前只有1个segment xhci_write_intr_reg32(id, 0, XHCI_IR_TABLE_SIZE, 1); // 当前只有1个segment
io_mfence();
xhci_write_intr_reg64(id, 0, XHCI_IR_DEQUEUE, virt_2_phys(xhci_hc[id].event_ring_vaddr) | (1 << 3)); // 写入dequeue寄存器并清除busy位写1就会清除 xhci_write_intr_reg64(id, 0, XHCI_IR_DEQUEUE, virt_2_phys(xhci_hc[id].event_ring_vaddr) | (1 << 3)); // 写入dequeue寄存器并清除busy位写1就会清除
io_mfence();
xhci_write_intr_reg64(id, 0, XHCI_IR_TABLE_ADDR, virt_2_phys(xhci_hc[id].event_ring_table_vaddr)); // 写入table地址 xhci_write_intr_reg64(id, 0, XHCI_IR_TABLE_ADDR, virt_2_phys(xhci_hc[id].event_ring_table_vaddr)); // 写入table地址
io_mfence();
// 清除状态位 // 清除状态位
xhci_write_op_reg32(id, XHCI_OPS_USBSTS, (1 << 10) | (1 << 4) | (1 << 3) | (1 << 2)); xhci_write_op_reg32(id, XHCI_OPS_USBSTS, (1 << 10) | (1 << 4) | (1 << 3) | (1 << 2));
io_mfence();
// 开启usb中断 // 开启usb中断
// 注册中断处理程序 // 注册中断处理程序
struct xhci_hc_irq_install_info_t install_info; struct xhci_hc_irq_install_info_t install_info;
@ -770,7 +823,9 @@ static int xhci_hc_init_intr(int id)
char *buf = (char *)kmalloc(16, 0); char *buf = (char *)kmalloc(16, 0);
memset(buf, 0, 16); memset(buf, 0, 16);
sprintk(buf, "xHCI HC%d", id); sprintk(buf, "xHCI HC%d", id);
io_mfence();
irq_register(xhci_controller_irq_num[id], &install_info, &xhci_hc_irq_handler, id, &xhci_hc_intr_controller, buf); irq_register(xhci_controller_irq_num[id], &install_info, &xhci_hc_irq_handler, id, &xhci_hc_intr_controller, buf);
io_mfence();
kfree(buf); kfree(buf);
kdebug("xhci host controller %d: interrupt registered. irq num=%d", id, xhci_controller_irq_num[id]); kdebug("xhci host controller %d: interrupt registered. irq num=%d", id, xhci_controller_irq_num[id]);
@ -794,7 +849,7 @@ void xhci_init(struct pci_device_structure_general_device_t *dev_hdr)
spin_lock(&xhci_controller_init_lock); spin_lock(&xhci_controller_init_lock);
kinfo("Initializing xhci host controller: bus=%#02x, device=%#02x, func=%#02x, VendorID=%#04x, irq_line=%d, irq_pin=%d", dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, dev_hdr->header.Vendor_ID, dev_hdr->Interrupt_Line, dev_hdr->Interrupt_PIN); kinfo("Initializing xhci host controller: bus=%#02x, device=%#02x, func=%#02x, VendorID=%#04x, irq_line=%d, irq_pin=%d", dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, dev_hdr->header.Vendor_ID, dev_hdr->Interrupt_Line, dev_hdr->Interrupt_PIN);
io_mfence();
int cid = xhci_hc_find_available_id(); int cid = xhci_hc_find_available_id();
if (cid < 0) if (cid < 0)
{ {
@ -805,13 +860,14 @@ void xhci_init(struct pci_device_structure_general_device_t *dev_hdr)
memset(&xhci_hc[cid], 0, sizeof(struct xhci_host_controller_t)); memset(&xhci_hc[cid], 0, sizeof(struct xhci_host_controller_t));
xhci_hc[cid].controller_id = cid; xhci_hc[cid].controller_id = cid;
xhci_hc[cid].pci_dev_hdr = dev_hdr; xhci_hc[cid].pci_dev_hdr = dev_hdr;
io_mfence();
pci_write_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0x4, 0x0006); // mem I/O access enable and bus master enable pci_write_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0x4, 0x0006); // mem I/O access enable and bus master enable
io_mfence();
// 为当前控制器映射寄存器地址空间 // 为当前控制器映射寄存器地址空间
xhci_hc[cid].vbase = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + XHCI_MAPPING_OFFSET + 65536 * xhci_hc[cid].controller_id; xhci_hc[cid].vbase = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + XHCI_MAPPING_OFFSET + 65536 * xhci_hc[cid].controller_id;
// kdebug("dev_hdr->BAR0 & (~0xf)=%#018lx", dev_hdr->BAR0 & (~0xf)); // kdebug("dev_hdr->BAR0 & (~0xf)=%#018lx", dev_hdr->BAR0 & (~0xf));
mm_map_phys_addr(xhci_hc[cid].vbase, dev_hdr->BAR0 & (~0xf), 65536, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, true); mm_map_phys_addr(xhci_hc[cid].vbase, dev_hdr->BAR0 & (~0xf), 65536, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, true);
io_mfence();
// 读取xhci控制寄存器 // 读取xhci控制寄存器
uint16_t iversion = *(uint16_t *)(xhci_hc[cid].vbase + XHCI_CAPS_HCIVERSION); uint16_t iversion = *(uint16_t *)(xhci_hc[cid].vbase + XHCI_CAPS_HCIVERSION);
@ -828,9 +884,11 @@ void xhci_init(struct pci_device_structure_general_device_t *dev_hdr)
// kdebug("hcc1.xECP=%#010lx", hcc1.xECP); // kdebug("hcc1.xECP=%#010lx", hcc1.xECP);
// 计算operational registers的地址 // 计算operational registers的地址
xhci_hc[cid].vbase_op = xhci_hc[cid].vbase + xhci_read_cap_reg8(cid, XHCI_CAPS_CAPLENGTH); xhci_hc[cid].vbase_op = xhci_hc[cid].vbase + xhci_read_cap_reg8(cid, XHCI_CAPS_CAPLENGTH);
io_mfence();
xhci_hc[cid].db_offset = xhci_read_cap_reg32(cid, XHCI_CAPS_DBOFF) & (~0x3); // bits [1:0] reserved xhci_hc[cid].db_offset = xhci_read_cap_reg32(cid, XHCI_CAPS_DBOFF) & (~0x3); // bits [1:0] reserved
io_mfence();
xhci_hc[cid].rts_offset = xhci_read_cap_reg32(cid, XHCI_CAPS_RTSOFF) & (~0x1f); // bits [4:0] reserved. xhci_hc[cid].rts_offset = xhci_read_cap_reg32(cid, XHCI_CAPS_RTSOFF) & (~0x1f); // bits [4:0] reserved.
io_mfence();
xhci_hc[cid].ext_caps_off = 1UL * (hcc1.xECP) * 4; xhci_hc[cid].ext_caps_off = 1UL * (hcc1.xECP) * 4;
xhci_hc[cid].context_size = (hcc1.csz) ? 64 : 32; xhci_hc[cid].context_size = (hcc1.csz) ? 64 : 32;
@ -849,25 +907,27 @@ void xhci_init(struct pci_device_structure_general_device_t *dev_hdr)
pci_write_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0xd8, 0xffffffff); pci_write_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0xd8, 0xffffffff);
pci_write_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0xd0, 0xffffffff); pci_write_config(dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func, 0xd0, 0xffffffff);
} }
io_mfence();
// 关闭legacy支持 // 关闭legacy支持
FAIL_ON_TO(xhci_hc_stop_legacy(cid), failed); FAIL_ON_TO(xhci_hc_stop_legacy(cid), failed);
io_mfence();
// 重置xhci控制器 // 重置xhci控制器
FAIL_ON_TO(xhci_hc_reset(cid), failed); FAIL_ON_TO(xhci_hc_reset(cid), failed);
io_mfence();
// 端口配对 // 端口配对
FAIL_ON_TO(xhci_hc_pair_ports(cid), failed); FAIL_ON_TO(xhci_hc_pair_ports(cid), failed);
io_mfence();
// ========== 设置USB host controller ========= // ========== 设置USB host controller =========
// 获取页面大小 // 获取页面大小
kdebug("ops pgsize=%#010lx", xhci_read_op_reg32(cid, XHCI_OPS_PAGESIZE)); kdebug("ops pgsize=%#010lx", xhci_read_op_reg32(cid, XHCI_OPS_PAGESIZE));
xhci_hc[cid].page_size = (xhci_read_op_reg32(cid, XHCI_OPS_PAGESIZE) & 0xffff) << 12; xhci_hc[cid].page_size = (xhci_read_op_reg32(cid, XHCI_OPS_PAGESIZE) & 0xffff) << 12;
kdebug("page size=%d", xhci_hc[cid].page_size); kdebug("page size=%d", xhci_hc[cid].page_size);
io_mfence();
// 获取设备上下文空间 // 获取设备上下文空间
xhci_hc[cid].dcbaap_vaddr = (uint64_t)kmalloc(2048, 0); // 分配2KB的设备上下文地址数组空间 xhci_hc[cid].dcbaap_vaddr = (uint64_t)kmalloc(2048, 0); // 分配2KB的设备上下文地址数组空间
memset((void *)xhci_hc[cid].dcbaap_vaddr, 0, 2048); memset((void *)xhci_hc[cid].dcbaap_vaddr, 0, 2048);
io_mfence();
kdebug("dcbaap_vaddr=%#018lx", xhci_hc[cid].dcbaap_vaddr); kdebug("dcbaap_vaddr=%#018lx", xhci_hc[cid].dcbaap_vaddr);
if (unlikely(!xhci_is_aligned64(xhci_hc[cid].dcbaap_vaddr))) // 地址不是按照64byte对齐 if (unlikely(!xhci_is_aligned64(xhci_hc[cid].dcbaap_vaddr))) // 地址不是按照64byte对齐
{ {
@ -876,7 +936,7 @@ void xhci_init(struct pci_device_structure_general_device_t *dev_hdr)
} }
// 写入dcbaap // 写入dcbaap
xhci_write_op_reg64(cid, XHCI_OPS_DCBAAP, virt_2_phys(xhci_hc[cid].dcbaap_vaddr)); xhci_write_op_reg64(cid, XHCI_OPS_DCBAAP, virt_2_phys(xhci_hc[cid].dcbaap_vaddr));
io_mfence();
// 创建command ring // 创建command ring
xhci_hc[cid].cmd_ring_vaddr = xhci_create_ring(XHCI_CMND_RING_TRBS); xhci_hc[cid].cmd_ring_vaddr = xhci_create_ring(XHCI_CMND_RING_TRBS);
if (unlikely(!xhci_is_aligned64(xhci_hc[cid].cmd_ring_vaddr))) // 地址不是按照64byte对齐 if (unlikely(!xhci_is_aligned64(xhci_hc[cid].cmd_ring_vaddr))) // 地址不是按照64byte对齐
@ -887,17 +947,21 @@ void xhci_init(struct pci_device_structure_general_device_t *dev_hdr)
// 设置初始cycle bit为1 // 设置初始cycle bit为1
xhci_hc[cid].cmd_trb_cycle = XHCI_TRB_CYCLE_ON; xhci_hc[cid].cmd_trb_cycle = XHCI_TRB_CYCLE_ON;
io_mfence();
// 写入command ring控制寄存器 // 写入command ring控制寄存器
xhci_write_op_reg64(cid, XHCI_OPS_CRCR, virt_2_phys(xhci_hc[cid].cmd_ring_vaddr) | xhci_hc[cid].cmd_trb_cycle); xhci_write_op_reg64(cid, XHCI_OPS_CRCR, virt_2_phys(xhci_hc[cid].cmd_ring_vaddr) | xhci_hc[cid].cmd_trb_cycle);
// 写入配置寄存器 // 写入配置寄存器
uint32_t max_slots = hcs1.max_slots; uint32_t max_slots = hcs1.max_slots;
kdebug("max slots = %d", max_slots); kdebug("max slots = %d", max_slots);
io_mfence();
xhci_write_op_reg32(cid, XHCI_OPS_CONFIG, max_slots); xhci_write_op_reg32(cid, XHCI_OPS_CONFIG, max_slots);
io_mfence();
// 写入设备通知控制寄存器 // 写入设备通知控制寄存器
xhci_write_op_reg32(cid, XHCI_OPS_DNCTRL, (1 << 1)); // 目前只有N1被支持 xhci_write_op_reg32(cid, XHCI_OPS_DNCTRL, (1 << 1)); // 目前只有N1被支持
io_mfence();
FAIL_ON_TO(xhci_hc_init_intr(cid), failed_free_dyn); FAIL_ON_TO(xhci_hc_init_intr(cid), failed_free_dyn);
io_mfence();
++xhci_ctrl_count; ++xhci_ctrl_count;
spin_unlock(&xhci_controller_init_lock); spin_unlock(&xhci_controller_init_lock);
return; return;
@ -916,9 +980,10 @@ failed_free_dyn:; // 释放动态申请的内存
kfree((void *)xhci_hc[cid].event_ring_vaddr); kfree((void *)xhci_hc[cid].event_ring_vaddr);
failed:; failed:;
io_mfence();
// 取消地址映射 // 取消地址映射
mm_unmap(xhci_hc[cid].vbase, 65536); mm_unmap(xhci_hc[cid].vbase, 65536);
io_mfence();
// 清空数组 // 清空数组
memset((void *)&xhci_hc[cid], 0, sizeof(struct xhci_host_controller_t)); memset((void *)&xhci_hc[cid], 0, sizeof(struct xhci_host_controller_t));
@ -926,4 +991,4 @@ failed_exceed_max:;
kerror("Failed to initialize controller: bus=%d, dev=%d, func=%d", dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func); kerror("Failed to initialize controller: bus=%d, dev=%d, func=%d", dev_hdr->header.bus, dev_hdr->header.device, dev_hdr->header.func);
spin_unlock(&xhci_controller_init_lock); spin_unlock(&xhci_controller_init_lock);
} }
#pragma GCC optimize("O0") #pragma GCC pop_options

View File

@ -1,4 +1,3 @@
// #pragma GCC optimize ("O0")
#include "trap.h" #include "trap.h"
#include "gate.h" #include "gate.h"
#include <process/ptrace.h> #include <process/ptrace.h>

View File

@ -1,8 +1,6 @@
#include "slab.h" #include "slab.h"
#include <common/compiler.h> #include <common/compiler.h>
#pragma GCC push_options
#pragma GCC optimize("O0")
struct slab kmalloc_cache_group[16] = struct slab kmalloc_cache_group[16] =
{ {
{32, 0, 0, NULL, NULL, NULL, NULL}, {32, 0, 0, NULL, NULL, NULL, NULL},
@ -707,4 +705,3 @@ unsigned long kfree(void *address)
kBUG("kfree(): Can't free memory."); kBUG("kfree(): Can't free memory.");
return ECANNOT_FREE_MEM; return ECANNOT_FREE_MEM;
} }
#pragma GCC pop_options

View File

@ -18,8 +18,7 @@
#include <filesystem/VFS/VFS.h> #include <filesystem/VFS/VFS.h>
#include <common/wait_queue.h> #include <common/wait_queue.h>
// #pragma GCC push_options
// #pragma GCC optimize("O0")
// 进程最大可拥有的文件描述符数量 // 进程最大可拥有的文件描述符数量
#define PROC_MAX_FD_NUM 16 #define PROC_MAX_FD_NUM 16

View File

@ -1,4 +1,3 @@
//#pragma GCC optimize("O0")
#include "../common/asm.h" #include "../common/asm.h"

View File

@ -10,8 +10,7 @@
#include <sched/sched.h> #include <sched/sched.h>
#include "ipi.h" #include "ipi.h"
#pragma GCC push_options
#pragma GCC optimize("O1")
void ipi_0xc8_handler(uint64_t irq_num, uint64_t param, struct pt_regs *regs); // 由BSP转发的HPET中断处理函数 void ipi_0xc8_handler(uint64_t irq_num, uint64_t param, struct pt_regs *regs); // 由BSP转发的HPET中断处理函数
static spinlock_t multi_core_starting_lock; // 多核启动锁 static spinlock_t multi_core_starting_lock; // 多核启动锁
@ -31,21 +30,25 @@ void smp_init()
// kdebug("processor num=%d", total_processor_num); // kdebug("processor num=%d", total_processor_num);
for (int i = 0; i < total_processor_num; ++i) for (int i = 0; i < total_processor_num; ++i)
{
io_mfence();
proc_local_apic_structs[i] = (struct acpi_Processor_Local_APIC_Structure_t *)(tmp_vaddr[i]); proc_local_apic_structs[i] = (struct acpi_Processor_Local_APIC_Structure_t *)(tmp_vaddr[i]);
}
//*(uchar *)0x20000 = 0xf4; // 在内存的0x20000处写入HLT指令(AP处理器会执行物理地址0x20000的代码) //*(uchar *)0x20000 = 0xf4; // 在内存的0x20000处写入HLT指令(AP处理器会执行物理地址0x20000的代码)
// 将引导程序复制到物理地址0x20000处 // 将引导程序复制到物理地址0x20000处
memcpy((unsigned char *)phys_2_virt(0x20000), _apu_boot_start, (unsigned long)&_apu_boot_end - (unsigned long)&_apu_boot_start); memcpy((unsigned char *)phys_2_virt(0x20000), _apu_boot_start, (unsigned long)&_apu_boot_end - (unsigned long)&_apu_boot_start);
io_mfence();
// 设置多核IPI中断门 // 设置多核IPI中断门
for (int i = 200; i < 210; ++i) for (int i = 200; i < 210; ++i)
set_intr_gate(i, 0, SMP_interrupt_table[i - 200]); set_intr_gate(i, 0, SMP_interrupt_table[i - 200]);
memset((void *)SMP_IPI_desc, 0, sizeof(irq_desc_t) * SMP_IRQ_NUM); memset((void *)SMP_IPI_desc, 0, sizeof(irq_desc_t) * SMP_IRQ_NUM);
io_mfence();
// 注册接收bsp处理器的hpet中断转发的处理函数 // 注册接收bsp处理器的hpet中断转发的处理函数
ipi_regiserIPI(0xc8, NULL, &ipi_0xc8_handler, NULL, NULL, "IPI 0xc8"); ipi_regiserIPI(0xc8, NULL, &ipi_0xc8_handler, NULL, NULL, "IPI 0xc8");
io_mfence();
ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x00, ICR_INIT, ICR_ALL_EXCLUDE_Self, true, 0x00); ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x00, ICR_INIT, ICR_ALL_EXCLUDE_Self, true, 0x00);
kdebug("total_processor_num=%d", total_processor_num); kdebug("total_processor_num=%d", total_processor_num);
for (int i = 1; i < total_processor_num; ++i) // i从1开始不初始化bsp for (int i = 1; i < total_processor_num; ++i) // i从1开始不初始化bsp
@ -53,22 +56,25 @@ void smp_init()
io_mfence(); io_mfence();
if (proc_local_apic_structs[i]->ACPI_Processor_UID == 0) if (proc_local_apic_structs[i]->ACPI_Processor_UID == 0)
--total_processor_num; --total_processor_num;
io_mfence();
if (proc_local_apic_structs[i]->local_apic_id > total_processor_num) if (proc_local_apic_structs[i]->local_apic_id > total_processor_num)
{ {
--total_processor_num; --total_processor_num;
continue; continue;
} }
kdebug("[core %d] acpi processor UID=%d, APIC ID=%d, flags=%#010lx", i, proc_local_apic_structs[i]->ACPI_Processor_UID, proc_local_apic_structs[i]->local_apic_id, proc_local_apic_structs[i]->flags); kdebug("[core %d] acpi processor UID=%d, APIC ID=%d, flags=%#010lx", i, proc_local_apic_structs[i]->ACPI_Processor_UID, proc_local_apic_structs[i]->local_apic_id, proc_local_apic_structs[i]->flags);
io_mfence();
spin_lock(&multi_core_starting_lock); spin_lock(&multi_core_starting_lock);
preempt_enable(); // 由于ap处理器的pcb与bsp的不同因此ap处理器放锁时bsp的自旋锁持有计数不会发生改变,需要手动恢复preempt count preempt_enable(); // 由于ap处理器的pcb与bsp的不同因此ap处理器放锁时bsp的自旋锁持有计数不会发生改变,需要手动恢复preempt count
current_starting_cpu = proc_local_apic_structs[i]->local_apic_id; current_starting_cpu = proc_local_apic_structs[i]->local_apic_id;
io_mfence();
// 为每个AP处理器分配栈空间 // 为每个AP处理器分配栈空间
cpu_core_info[current_starting_cpu].stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE; cpu_core_info[current_starting_cpu].stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
cpu_core_info[current_starting_cpu].ist_stack_start = (uint64_t)(kmalloc(STACK_SIZE, 0)) + STACK_SIZE; cpu_core_info[current_starting_cpu].ist_stack_start = (uint64_t)(kmalloc(STACK_SIZE, 0)) + STACK_SIZE;
io_mfence();
memset((void *)cpu_core_info[current_starting_cpu].stack_start - STACK_SIZE, 0, STACK_SIZE); memset((void *)cpu_core_info[current_starting_cpu].stack_start - STACK_SIZE, 0, STACK_SIZE);
memset((void *)cpu_core_info[current_starting_cpu].ist_stack_start - STACK_SIZE, 0, STACK_SIZE); memset((void *)cpu_core_info[current_starting_cpu].ist_stack_start - STACK_SIZE, 0, STACK_SIZE);
io_mfence();
// 设置ap处理器的中断栈及内核栈中的cpu_id // 设置ap处理器的中断栈及内核栈中的cpu_id
((struct process_control_block *)(cpu_core_info[current_starting_cpu].stack_start - STACK_SIZE))->cpu_id = proc_local_apic_structs[i]->local_apic_id; ((struct process_control_block *)(cpu_core_info[current_starting_cpu].stack_start - STACK_SIZE))->cpu_id = proc_local_apic_structs[i]->local_apic_id;
@ -82,15 +88,15 @@ void smp_init()
io_mfence(); io_mfence();
set_tss64((uint *)cpu_core_info[current_starting_cpu].tss_vaddr, cpu_core_info[current_starting_cpu].stack_start, cpu_core_info[current_starting_cpu].stack_start, cpu_core_info[current_starting_cpu].stack_start, set_tss64((uint *)cpu_core_info[current_starting_cpu].tss_vaddr, cpu_core_info[current_starting_cpu].stack_start, cpu_core_info[current_starting_cpu].stack_start, cpu_core_info[current_starting_cpu].stack_start,
cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start); cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start);
io_mfence();
// 连续发送两次start-up IPI // 连续发送两次start-up IPI
ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x20, ICR_Start_up, ICR_No_Shorthand, true, proc_local_apic_structs[i]->local_apic_id); ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x20, ICR_Start_up, ICR_No_Shorthand, true, proc_local_apic_structs[i]->local_apic_id);
io_mfence();
ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x20, ICR_Start_up, ICR_No_Shorthand, true, proc_local_apic_structs[i]->local_apic_id); ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x20, ICR_Start_up, ICR_No_Shorthand, true, proc_local_apic_structs[i]->local_apic_id);
} }
io_mfence();
while (num_cpu_started != total_processor_num) while (num_cpu_started != total_processor_num)
__asm__ __volatile__("pause" :: pause();
: "memory");
kinfo("Cleaning page table remapping...\n"); kinfo("Cleaning page table remapping...\n");
@ -98,6 +104,7 @@ void smp_init()
uint64_t *global_CR3 = get_CR3(); uint64_t *global_CR3 = get_CR3();
for (int i = 0; i < 256; ++i) for (int i = 0; i < 256; ++i)
{ {
io_mfence();
*(ul *)(phys_2_virt(global_CR3) + i) = 0UL; *(ul *)(phys_2_virt(global_CR3) + i) = 0UL;
} }
kdebug("init proc's preempt_count=%ld", current_pcb->preempt_count); kdebug("init proc's preempt_count=%ld", current_pcb->preempt_count);
@ -123,7 +130,7 @@ void smp_ap_start()
__asm__ __volatile__("movq %0, %%rsp \n\t" ::"m"(stack_start) __asm__ __volatile__("movq %0, %%rsp \n\t" ::"m"(stack_start)
: "memory");*/ : "memory");*/
ksuccess("AP core successfully started!"); ksuccess("AP core successfully started!");
io_mfence();
++num_cpu_started; ++num_cpu_started;
kdebug("current cpu = %d", current_starting_cpu); kdebug("current cpu = %d", current_starting_cpu);
@ -157,6 +164,7 @@ void smp_ap_start()
// kdebug("IDT_addr = %#018lx", phys_2_virt(IDT_Table)); // kdebug("IDT_addr = %#018lx", phys_2_virt(IDT_Table));
spin_unlock(&multi_core_starting_lock); spin_unlock(&multi_core_starting_lock);
preempt_disable(); // 由于ap处理器的pcb与bsp的不同因此ap处理器放锁时需要手动恢复preempt count preempt_disable(); // 由于ap处理器的pcb与bsp的不同因此ap处理器放锁时需要手动恢复preempt count
io_mfence();
sti(); sti();
while (1) while (1)
@ -179,5 +187,3 @@ void ipi_0xc8_handler(uint64_t irq_num, uint64_t param, struct pt_regs *regs)
{ {
sched_update_jiffies(); sched_update_jiffies();
} }
#pragma GCC optimize("O0")