ARM_SMMU_下

article/2025/9/18 11:03:57

SMMU驱动代码分析

本文主要分析linux kernel中SMMUv3的代码(drivers/iommu/arm-smmu-v3.c)
linux kernel版本是linux 5.7, 体系结构是aarch64

SMMU的作用是把CPU提交给设备的VA地址,直接作为设备发出的地址,变成正确的物理地址,访问到物理内存上。和 mmu 不同的是,一个 smmu 可以有多个设备连着,他们的页表不可能复用,SMMU 用 stream id 作区分。一个设备有多个进程,所以 smmu 单元也要支持多页表,smmu 使用substream id 区分多进程的页表。

smmu的设备节点定义

在讨论smmu的代码前,先看下smmu的设备节点是怎么定义的

smmu@2b400000 {compatible = "arm,smmu-v3";reg = <0x0 0x2b400000 0x0 0x20000>;interrupts = <GIC_SPI 74 IRQ_TYPE_EDGE_RISING>,<GIC_SPI 75 IRQ_TYPE_EDGE_RISING>,<GIC_SPI 77 IRQ_TYPE_EDGE_RISING>,<GIC_SPI 79 IRQ_TYPE_EDGE_RISING>;interrupt-names = "eventq", "priq", "cmdq-sync", "gerror";dma-coherent;#iommu-cells = <1>;msi-parent = <&its 0xff0000>;};

compatible: 用于匹配smmu驱动。
reg:smmu设备的物理基地址。
interrupts: 描述与中断名称对应的smmu中断源,上述分别对应中断类型,中断号以及中断触发方式。
interrupt-names: 中断名称。
eventq,当event queue从空变为非空状态时上报中断。
priq, 当pri queue从空变为非空状态时上报中断。
cmdq-sync, command queue中CMDQ_SYNC命令完成时产生中断。
gerror,event记录到event queue过程中产生的错误会记录在SMMU_GERROR寄存器中,并产生中断。
combined,组合中断,需要硬件支持,如果提供了组合中断,则将优先使用组合中断。
dma-coherent:表示设备通过smmu进行的DMA访问是否cache coherent的,假设DMA把外设的数据搬运到内存的某个位置,cpu去读那段地址,因为cache命中了,读到的还是旧的值,这就是cache的不coherent。
#iommu-cells: 一个cell代表一个streamid, smmu-v3必须定义为1。
msi-parent:指定msi中断控制器。

struct arm_smmu_domain {struct arm_smmu_device      *smmu;struct mutex            init_mutex; /* Protects smmu pointer */struct io_pgtable_ops       *pgtbl_ops;bool                non_strict;atomic_t            nr_ats_masters;enum arm_smmu_domain_stage  stage;union {struct arm_smmu_s1_cfg  s1_cfg;struct arm_smmu_s2_cfg  s2_cfg;};struct iommu_domain     domain;struct list_head        devices;spinlock_t          devices_lock;
};

arm_smmu_device: 指定smmu设备
io_pgtable_ops: io页表映射定义的一系列操作
non_strict: smmu non-strict模式,在该补丁集中引入 add non-strict mode support for arm-smmu-v3,
主要是为了解决开启smmu后,频繁的unmap,需要频繁的invalid tlb带来的性能损失, 所以不在每一次unmap后都进行tlb invalidate操作,而是累计一定次数或者时间后执行invalid all操作,但这样是有一定的安全风险(页表虽然释放了但是还是在tlb中有残留,可能被利用到)。可以通过启动参数控制。
nr_ats_masters: ats的设备数量,enable_ats时数量+1, disable ats时数量减1
arm_smmu_domain_stage: 代表smmu支持的方式,支持stage1的转换,stage2的转换,stage1 + stage2的转换,以及bypass模式。
arm_smmu_s1_cfg: stage1转换需要的数据结构
arm_smmu_s2_cfg: stage2转换需要的数据结构

smmu驱动初始化

static int arm_smmu_device_probe(struct platform_device *pdev)
{int irq, ret;struct resource *res;resource_size_t ioaddr;struct arm_smmu_device *smmu;struct device *dev = &pdev->dev;bool bypass;smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);if (!smmu)return -ENOMEM;smmu->dev = dev;if (dev->of_node) {ret = arm_smmu_device_dt_probe(pdev, smmu);} else {ret = arm_smmu_device_acpi_probe(pdev, smmu);if (ret == -ENODEV)return ret;}/* Set bypass mode according to firmware probing result */bypass = !!ret;/* Base address */res = platform_get_resource(pdev, IORESOURCE_MEM, 0);if (!res)return -EINVAL;if (resource_size(res) < arm_smmu_resource_size(smmu)) {dev_err(dev, "MMIO region too small (%pr)\n", res);return -EINVAL;}ioaddr = res->start;/** Don't map the IMPLEMENTATION DEFINED regions, since they may contain* the PMCG registers which are reserved by the PMU driver.*/smmu->base = arm_smmu_ioremap(dev, ioaddr, ARM_SMMU_REG_SZ);if (IS_ERR(smmu->base))return PTR_ERR(smmu->base);if (arm_smmu_resource_size(smmu) > SZ_64K) {smmu->page1 = arm_smmu_ioremap(dev, ioaddr + SZ_64K,ARM_SMMU_REG_SZ);if (IS_ERR(smmu->page1))return PTR_ERR(smmu->page1);} else {smmu->page1 = smmu->base;}/* Interrupt lines */irq = platform_get_irq_byname_optional(pdev, "combined");if (irq > 0)smmu->combined_irq = irq;else {irq = platform_get_irq_byname_optional(pdev, "eventq");if (irq > 0)smmu->evtq.q.irq = irq;irq = platform_get_irq_byname_optional(pdev, "priq");if (irq > 0)smmu->priq.q.irq = irq;irq = platform_get_irq_byname_optional(pdev, "gerror");if (irq > 0)smmu->gerr_irq = irq;}/* Probe the h/w */ret = arm_smmu_device_hw_probe(smmu);if (ret)return ret;/* Initialise in-memory data structures */ret = arm_smmu_init_structures(smmu);if (ret)return ret;/* Record our private device structure */platform_set_drvdata(pdev, smmu);/* Check for RMRs and install bypass STEs if any */arm_smmu_rmr_install_bypass_ste(smmu);/* Reset the device */ret = arm_smmu_device_reset(smmu, bypass);if (ret)return ret;/* And we're up. Go go go! */ret = iommu_device_sysfs_add(&smmu->iommu, dev, NULL,"smmu3.%pa", &ioaddr);if (ret)return ret;ret = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev);if (ret) {dev_err(dev, "Failed to register iommu\n");goto err_sysfs_remove;}ret = arm_smmu_set_bus_ops(&arm_smmu_ops);if (ret)goto err_unregister_device;return 0;err_unregister_device:iommu_device_unregister(&smmu->iommu);
err_sysfs_remove:iommu_device_sysfs_remove(&smmu->iommu);return ret;
}
+->arm_smmu_device_probe() //smmu设备驱动probe入口函数+-> arm_smmu_device_dt_probe() //smmu设备树解析+-> platform_get_irq_byname() // smmu设备中断解析+-> arm_smmu_device_hw_probe() // smmu硬件规格探测+-> arm_smmu_init_structures() //smmu 数据结构初始化+-> arm_smmu_device_reset() // smmu设备复位, 硬件初始化配置+-> iommu_device_register() // iommu设备注册+-> arm_smmu_set_bus_ops() // 给支持的总线设置bus->iommu_ops

1.arm_smmu_device_dt_probe

static int arm_smmu_device_dt_probe(struct platform_device *pdev,struct arm_smmu_device *smmu)
{struct device *dev = &pdev->dev;u32 cells;int ret = -EINVAL;if (of_property_read_u32(dev->of_node, "#iommu-cells", &cells))dev_err(dev, "missing #iommu-cells property\n");else if (cells != 1)dev_err(dev, "invalid #iommu-cells value (%d)\n", cells);elseret = 0;parse_driver_options(smmu);if (of_dma_is_coherent(dev->of_node))smmu->features |= ARM_SMMU_FEAT_COHERENCY;return ret;
}

a. 读取设备树,看smmu的设备节点定义中#iommu-cells是否为1, 如果不为1则直接bypass 掉smmu

b. parse_driver_options, 主要解析smmu是否有需要规避的硬件bug

c. 解析smmu设备中的dma-coherent属性

2. platform_get_irq_byname

/* Interrupt lines */irq = platform_get_irq_byname_optional(pdev, "combined");  if (irq > 0)smmu->combined_irq = irq;else {irq = platform_get_irq_byname_optional(pdev, "eventq");if (irq > 0)smmu->evtq.q.irq = irq;irq = platform_get_irq_byname_optional(pdev, "priq");if (irq > 0)smmu->priq.q.irq = irq;irq = platform_get_irq_byname_optional(pdev, "gerror");if (irq > 0)smmu->gerr_irq = irq;}

分别获取dts节点中定义的"combined", “eventq”, “priq”, "gerror"中断号

3.arm_smmu_device_hw_probe

该函数主要探测smmu设备的硬件规格,主要是通过读SMMU的IDR0,IDR1,IDR5寄存器确认

4.arm_smmu_init_structures

smmu相关的数据结构的内存申请和初始化

static int arm_smmu_init_structures(struct arm_smmu_device *smmu)
{int ret;ret = arm_smmu_init_queues(smmu); ----------------- (a)if (ret)return ret;return arm_smmu_init_strtab(smmu);  ----------------- (b)
}

(a) arm_smmu_init_queues()
会初始化三个queue, 分别为cmd queue, event queue, pri queue.
SMMU使用这3个队列做基本的事件管理。
event queue用于记录软件配置错误的状态信息,smmu将配置错误信息记录到event queue中,软件会通过从event queue读取配置错误信息,然后进行相应的配置错误处理。

软件使用command queue和smmu 硬件进行交互,软件写命令发送到command queue中,smmu会从command queue中读取命令进行处理。

pri queue需要硬件支持pri 特性,和event queue类似,当有相应硬件事件发生时,硬件把相应的描述符写入pri queue, 然后上报中断。

(b) arm_smmu_init_strtab

static int arm_smmu_init_strtab(struct arm_smmu_device *smmu)
{u64 reg;int ret;if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)  ret = arm_smmu_init_strtab_2lvl(smmu);elseret = arm_smmu_init_strtab_linear(smmu);if (ret)return ret;/* Set the strtab base address */reg  = smmu->strtab_cfg.strtab_dma & STRTAB_BASE_ADDR_MASK;reg |= STRTAB_BASE_RA;smmu->strtab_cfg.strtab_base = reg;/* Allocate the first VMID for stage-2 bypass STEs */set_bit(0, smmu->vmid_map);return 0;
}

首先确认SMMU的stream table的组织方式是线性table还是2-level table.
如果是linear table:
在这里插入图片描述

**arm_smmu_init_strtab_linear**// 计算stream table的size, 如果使用linear 查找,stream table的size = sid * 64(sid表示有多少个ste, 一个STE的大小为64B)size = (1 << smmu->sid_bits) * (STRTAB_STE_DWORDS << 3);// 申请Stream table的内存strtab = dmam_alloc_coherent()// 配置stream table(STRTAB_BASE_CFG)的format, 决定stream table的格式是linearreg  = FIELD_PREP(STRTAB_BASE_CFG_FMT, STRTAB_BASE_CFG_FMT_LINEAR);// 配置stream table(STRTAB_BASE_CFG)的log2size, ste的entry数目是2 ^ log2sizereg |= FIELD_PREP(STRTAB_BASE_CFG_LOG2SIZE, smmu->sid_bits);// cfg->num_l1_ents对应的是sid, 对SMMU下的所有sid逐一调用arm_smmu_write_strtab_entarm_smmu_init_bypass_stes(strtab, cfg->num_l1_ents)

在这里插入图片描述
如果是2-level table:
在这里插入图片描述
先通过sid的高位找到L1_STD(STRTAB_BASE + sid[9:8] * 8, 一个L1_STD的大小为8B), L1_STD定义了下一级查找的基地址,然后通过sid 找到具体的STE(l2ptr + sid[7:0] * 64).

+-> arm_smmu_init_strtab_2lvl()/* 计算l1的大小, 一个l1 std的大小为8byte, 对应的l1_std = sid[maxbit:split], maxbit是log2Size - 1, 所以l1的大小等于2 ^ (log2Size - split) * 8  */l1size = cfg->num_l1_ents * (STRTAB_L1_DESC_DWORDS << 3);// 申请L1 stream table的空间strtab = dmam_alloc_coherent()// 配置stream table(STRTAB_BASE_CFG)的format, 决定stream table的格式是2-levelreg  = FIELD_PREP(STRTAB_BASE_CFG_FMT, STRTAB_BASE_CFG_FMT_2LVL);/* 配置stream table(STRTAB_BASE_CFG)的log2size,2级ste的entry是2 ^ log2size, l1 std的entry大小为2 ^ (log2size - split) */reg |= FIELD_PREP(STRTAB_BASE_CFG_LOG2SIZE, size);/* 配置stream table(STRTAB_BASE_CFG)的split, split的值可以被配置为6/8/10,分别对应l1 std能够指向的最大二级ste的空间为4k/16k/64k*/reg |= FIELD_PREP(STRTAB_BASE_CFG_SPLIT, STRTAB_SPLIT);/* 分配L1STD的内存, 并配置L1 descriptor的SPAN,SPAN表示L2 table包含多少个STE */arm_smmu_init_l1_strtab()

申请 L1 Stream table 的内存,内存大小为2 ^ (log2Size - split) * 8
申请 L1 STD 的内存, L1 STD在 stream table 的索引是 streamID[maxbit: split]
配置完stream table的结构和各级大小后,再配置stream table的基地址:
在这里插入图片描述
配置完 stream table 的结构和各级大小后,再配置 stream table 的基地址

5.arm_smmu_device_reset

该函数主要是进行smmu的硬件配置

	arm_smmu_device_reset()// 写SMMU_CR0来disable smmu,并通过SMMU_CR0ACK检查CR0是否被cleararm_smmu_device_disable()// 配置读取ste和command queue的属性writel_relaxed(ARM_SMMU_CR1);// random crapwritel_relaxed(ARM_SMMU_CR2);/* 配置ARM_SMMU_STRTAB_BASE和ARM_SMMU_STRTAB_BASE寄存器,分别对应stream table的物理基地址以及格式,大小等*/writeq_relaxed(smmu->strtab_cfg.strtab_base, ARM_SMMU_STRTAB_BASE);writel_relaxed(smmu->strtab_cfg.strtab_base_cfg, ARM_SMMU_STRTAB_BASE);/* 配置cmd queue相关寄存器* ARM_SMMU_CMDQ_BASE是配置command queue的基地址* ARM_SMMU_CMDQ_PROD, 可以表示取出命令的位置* ARM_SMMU_CMDQ_CONS, 可以表示输入命令的位置* ARM_SMMU_CMDQ_PROD和ARM_SMMU_CMDQ_CONS初始化时配置为相同的值,都为0* 通过CMDQ_PROD和CMDQ_CONS, 可以判断command queue是否还有空间*/writeq_relaxed(smmu->cmdq.q.q_base, smmu->base + ARM_SMMU_CMDQ_BASE);writel_relaxed(smmu->cmdq.q.llq.prod, smmu->base + ARM_SMMU_CMDQ_PROD);writel_relaxed(smmu->cmdq.q.llq.cons, smmu->base + ARM_SMMU_CMDQ_CONS);// 最后配置command queue的en,对command queue进行使能enables = CR0_CMDQEN;// 配置event queue相关寄存器, 流程和command queue类似config event queue// 如果支持pri, 则配置pri queue相关寄存器, 流程和上面一致config pri queue// 申请并使能smmu支持的相关中断(eventq irq, priq irq, gerror irq)arm_smmu_setup_irqs()// enable smmu, 写SMMU_CR0,并通过SMMU_CR0ACK检查CR0是否被enablearm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0, ARM_SMMU_CR0ACK);

再着重讲下smmu的中断注册:arm_smmu_setup_irqs()

+-> arm_smmu_setup_irqs()+-> arm_smmu_setup_unique_irqs()+-> arm_smmu_setup_msis(smmu);+->  arm_smmu_write_msi_msg()+-> devm_request_irq(smmu->dev, irq, arm_smmu_gerror_handler,0, "arm-smmu-v3-gerror", smmu);

arm_smmu_write_msi_msg()函数里会去:
配置MSI中断的目的地址
配置MSI的中断数据
配置MSI中断的写地址的属性
配置完成后,当中断产生时,最终会进入中断注册的处理函数, 以gerror的中断处理为例:

arm_smmu_gerror_handler()// 读gerror和gerrorrn寄存器,确认gerror中断发生的错误类型gerror = readl_relaxed(smmu->base + ARM_SMMU_GERROR);gerrorn = readl_relaxed(smmu->base + ARM_SMMU_GERRORN);// 完成中断处理后,写gerror和gerrorn对应的的位一致,global中断处理完成writel(gerror, smmu->base + ARM_SMMU_GERRORN);

6. iommu_device_register

注册iommu设备,主要设计一个操作,就是将smmu设备添加到iommu_device_list中

int iommu_device_register(struct iommu_device *iommu)
{spin_lock(&iommu_device_lock);list_add_tail(&iommu->list, &iommu_device_list);spin_unlock(&iommu_device_lock);return 0;
}

7.arm_smmu_set_bus_ops

给smmu支持的总线设置bus->iommu_ops, 让总线具有了iommu attach的能力
arm_smmu_set_bus_ops(&arm_smmu_ops)
bus_set_iommu(&pci_bus_type, ops);
bus_set_iommu(&amba_bustype, ops);
bus_set_iommu(&platform_bus_type, ops);

arm_smmu_ops结构体定义如下:
在这里插入图片描述
主要分析smmu的两个关键操作:arm_smmu_attach_dev和arm_smmu_add_device
arm_smmu_add_device: 将smmu设备添加到总线

arm_smmu_add_device()smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode);/* for each sid, 如果是2-level ste, 为l2 ste分配内存*在之前的init_l1_strtab, 已经初始化了L1_std, L1_STD定义了下一级查找的基地址,* 现在可以通过sid 找到具体的STE(l2ptr + sid[7:0] * 64)* 这个函数先为每一个sid分配L2_STE的内存, 分配完成后在为每一个SID进行cfg配置*/arm_smmu_init_l2_strtab()// 将device和group关联起来iommu_device_link()

总线扫描发现了设备,总线的发现流程负责调用iommu_ops(arm_smmu_ops )给这个设备加上iommu_group,然后让iommu_group指向对应的iommu控制器
arm_smmu_attach_dev, 尝试为设备寻找到驱动

arm_smmu_attach_dev()// 从iommu_domain 中得到arm_smmu_domainsmmu_domain = to_smmu_domain(iommu_domain );// 一般情况下smmu_domain->smmu = NULL// 在arm_smmu_add_device中,我们已经为STE项分配了内存arm_smmu_domain_finalise(domain, master);// 分配asidasids = arm_smmu_bitmap_alloc()// 根据smmu stage是stage1还是stage2, 如果smmu domain是stage1arm_smmu_domain_finalise_s1()// 分配CD table的空间arm_smmu_alloc_cd_tables(smmu_domain);// 配置CD descriptor的cfgcfg->cd.tcr = FIELD_PREP(CTXDESC_CD_0_XXX)...// 如果smmu domain是stage2, STE已经包含了页表的s2ttb基地址和vmid,结束arm_smmu_domain_finalise_s2()finalise_stage_fn(smmu_domain, master, &pgtbl_cfg);

在这里插入图片描述
结合代码分析:
在这里插入图片描述
在CD的建立过程中,主要涉及到以下几点:
ste.S1Contextptr中定义了CD的基地址,CD的大小为64byte
a. 需要配置ste.S1CDMax, cdmax为0表示这个ste只有一个CD, 不需要使用到substreamid, 如果cdmax不为0, 那么CD的数目是2 ^ S1CDMax;
b. 需要配置ste.S1Fmt, 如果是linear结构的CD,CD的获取方法为S1ContextPTR + 64 * ssid; 如果是2-level结构的CD, L1CD的索引为ssid[s1cdmax - 1: 6], L2CD的索引为ssid[5:0]

attach_dev完成后,如果是stage1相关,CD的结构,大小和基地址已经成功建立,成功获取STE后,可以通过substreamid找到CD(S1ContextPTR + 64 * ssid)。找到的CD中包含页表PTW需要的TTBR寄存器,所以每一个CD对应一个页表, 这样一个SMMU单元,就可以有多张页表。

总结:

smmu 驱动的初始化流程就是一个探测硬件规格,初始化硬件配置,分配 STD/STE/CD 等空间的过程。
在这里插入图片描述


http://chatgpt.dhexx.cn/article/y5J02tfK.shtml

相关文章

arm SMMU

相当于x86下面的IOMMU&#xff0c;是不同架构的不同实现。统一在linux中的IOMMU框架内部&#xff0c;都由结构体 iommu_device 管理。 SMMU的作用是将设备的DMA请求地址翻译成可用的物理地址&#xff0c;当不开启SMMU时也可以直接使用DMA来搬运内存。SMMU是为了安全考虑中间加…

smmu梳理

smmu 用途 用途&#xff1a; &#xff08;参考引用见 参考资料章节&#xff09; 访问非连续的地址 现在系统中很少再预留连续的memory&#xff0c;如果Master需要很多memory&#xff0c;可以通过SMMU把一些非连续的PA映射到连续的VA&#xff0c;例如给DMA&#xff0c;VPU&…

Android 底层知识-SMMU

1.首先了解下MMU MMU是Memory Management Unit的缩写&#xff0c;中文名是内存管理单元。它是一种负责处理中央处理器&#xff08;CPU&#xff09;的内存访问请求的计算机硬件。它的功能包括虚拟地址到物理地址的转换&#xff08;即虚拟内存管理&#xff09;、内存保护、中央处…

理解SMMU基本原理和基本概念

&#xff08;阅读本文前假设您已经熟悉了MMU工作原理&#xff09; 什么是SMMU/IOMMU, 有什么作用&#xff1a; SMMU 有MMU完全一样的能力&#xff0c;当设备以DMA方式工作时&#xff0c;连接在它上面的IOMMU/SMMU可以将设备所使用的地址进行进一步的转换&#xff0c;完全和MM…

SMMU 介绍

SMMU在系统中位置和作用 SMMU功能与MMU功能类似&#xff0c;将IO设备的DMA地址请求(IOVA)转化为系统总线地址(PA)&#xff0c;实现地址映射、属性转换、权限检查等功能&#xff0c;实现不同设备的DMA地址空间隔离。 SMMU软硬件交互过程 硬件结构如下图所示&#xff1a; SMMU处…

MMU和SMMU/IOMMU使用场景和区别,SMMU技术与cache

目录 1.各种MMU 2.各种MMU的使用场景 2.1 各自的使用场景 2.1.1 地址转换 2.1.2 内存保护 3.MMU和SMMU的区别 4. MMU/SMMU/IOMMU与cache 5.扩展阅读 1.各种MMU MMU是memory manage unit 内存管理单元&#xff1b; SMMU是system memory manage unit 系统内存管理单元&a…

junit runwith

junit的runwith是给单元测试中增加一些附属特性&#xff0c;今天在看flink源码的时候&#xff0c;发现其使用了一个注解RunWith(Parameterized.class)&#xff0c;有点好奇&#xff0c;于是研究了一下这个注解的使用。 这个注解产生的背景是这样的&#xff1a;flink中的每个功能…

springmvc--@runwith(springrunner.class)报错

目录 解决方法  1)代码 这是一个文件上传的测试类 不重要,重要的是 RunWith(SpringRunner.class)报错! import com.github.tobato.fastdfs.domain.StorePath; import com.github.tobato.fastdfs.domain.ThumbImageConfig; import com.github.tobato.fastdfs.service.FastF…

run()和start()方法区别

多线程原理&#xff1a;相当于玩游戏机&#xff0c;只有一个游戏机&#xff08;cpu&#xff09;&#xff0c;可是有很多人要玩&#xff0c;于是&#xff0c;start是排队&#xff01;等CPU选中你就是轮到你&#xff0c;你就run&#xff08;&#xff09;&#xff0c;当CPU的运行的…

关于@RunWith注解的一点问题

问题引入 今天在用IDEA写一个springboot测试的时候&#xff0c;碰到了一个问题。 SpringBootTest public class ServiceTest {Autowiredprivate IUserService userService;Testpublic void testSelectById(){User byId userService.getById(1);System.out.println(byId);} }…

Java学习笔记-@RunWith(SpringRunner.class)

我们在进行Test测试时&#xff0c;除了要保证测试的包和启动包是同一个路径下&#xff0c;并且需要在Test测试类上加上RunWith(SpringRunner.class)注解&#xff0c;否则的话会直接报一个NullPointerExecption的错误&#xff01; RunWith(SpringRunner.class)的作用&#xff1…

@RunWith和 SpringJUnit4ClassRunner ----junit4和Spring一起使用

今天在看Spring的Demo的时候&#xff0c;看到了如此单元测试的写法 如下: RunWIth(SpringJunit4ClassRunner.class) ContextConfiguration(locations {"classpath:applicationContext.xml"} public class MyTest { Test public void hehe() { //....... }…

@RunWith(SpringRunner.class)测试SpringRunner.class爆红

SpringRunner报红无法添加类&#xff0c;上pom.xml文件中去掉scope标签内容即可。如下图所示 然后再点击右边侧面的Reload All Maven Projects即可 方法2 如图所示&#xff0c;直接altenter强制 以上两种方法都不可用的时候&#xff0c;这个时候就得手动导入jar包了&#xf…

Java学习笔记-@RunWith(SpringRunner.class)的作用

建议阅读&#xff1a;https://blog.csdn.net/u011835956/article/details/113950577 注意&#xff1a;Test都是基于Junit写的case&#xff0c;并不是基于TestNg框架&#xff0c;如果是TestNg框架的话&#xff0c;应该要继承AbstractTestNGSpringContextTests&#xff0c;这样b…

SpringBoot在使用测试的时候是否需要@RunWith?

我们在使用SpringBoot进行测试的时候一般是需要加两个注解&#xff1a; SpringBootTest 目的是加载ApplicationContext&#xff0c;启动spring容器。 RunWith 是一个测试启动器&#xff0c;可以加载SpringBoot测试注解让测试在Spring容器环境下执行。如测试类中无此注解&#…

SpringBootTest单元测试—加入@Test无法启动测试方法,什么情况下需要加@RunWith(SpringRunner.class)?

spring环境下单元测试: SpringBoot环境下单元测试: 一、SpringBoot2.4.0之后 二、2.2.0 < SpringBoot < 2.4. 三、SpringBoot2.2.0之前 最近写SpringBootTest单元测试时&#xff0c;加入Test无法启动测试方法&#xff08;我用的是SpringBoot2.2.0之前的版本&#xff09;…

Run Run Run

学习了 2022 年集训队论文 《浅谈与 Lyndon 理论有关的字符串组合问题》 写得很好&#xff0c;像我这样的字符串小白也能看懂 Lyndon 分解 若字符串 w w w 小于它的每一个真后缀&#xff0c;则称 w w w 是 Lyndon 串。若字符串 w w w 是 Lyndon 串&#xff0c;则 w k w ′…

关于springboot在启动测试类时报错的问题和@RunWith的使用

【1】最重要的一点&#xff1a;测试类启动依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupI…

天呐!惊人的Springboot测试.Springboot测试类之@RunWith注解

Springboot测试类之@RunWith注解 Springboot测试类之@RunWith注解Springboot测试类之@RunWith注解 @runWith注解作用: @RunWith就是一个运行器@RunWith(JUnit4.class)就是指用JUnit4来运行@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境,以便在测试开…

SSM中使用测试类RunWith

RunWith(SpringJUnit4ClassRunner.class) 需要使用两个测试包 <!--测试包--><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.2.7.RELEASE</version></dependency>…